from docx import Document
from docx.enum.section import WD_ORIENT
from docx.shared import Mm
from docx.oxml.ns import qn
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Pt,RGBColor
from tkinter.simpledialog import askstring
import random
import os
import sys
import time
import re
import tkinter
import tkinter.messagebox
import getpass
import datetime
gongneng_flag=0
count = 0#每次生成新题型之前都归零一次，用于遍历当前题库计算总题数
list_tihao=[]#随机从总题数里面抽一个输入数量的数组用于从题库抽题
new_tihao_tk=0#用于指定新题号来替代填空题库里的老题号
new_tihao_xz=0#用于指定新题号来替代选择题库里的老题号
new_tihao_pd=0#用于指定新题号来替代判断题库里的老题号
new_tihao_jd=0#用于指定新题号来替代简答题库里的老题号
ti = ''#用于将题读取出来再写入试卷
count_tishu_tk = 0#用于输入指定填空题题数
count_tishu_xz = 0#用于输入指定的选择题数
choose_count=0#用于指定大题类型
tiankong_ok = 0
xuanze_ok = 0
panduan_ok=0
jianda_ok = 0
homepage_flag = 0
def check_tk_tiku():
    if os.path.exists(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\填空题题库.docx') == True:#获取当前软件程序所在文件夹中是否有填空题库文件
        print('已检测到【填空题】题库文件，【填空题】题库存在')
        #print('位置为：' + str(os.path.realpath(sys.argv[0])))#为了使用Pyinstaller打包后能识别当前文件夹的地址，必须使用此方法获取真实地址
        return 'tk_Y'
    else:
        print('\n未检测到【填空题】题库文件，【填空题】题库不存在')
        return 'tk_N'
def check_xz_tiku():
    if os.path.exists(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\选择题题库.docx') == True:#获取当前软件程序所在文件夹中是否有选择题库文件
        print('已检测到【选择题】题库文件，【选择题】题库存在')
        #print('位置为：' + str(os.path.dirname(os.path.realpath(sys.argv[0]))))
        return 'xz_Y'
    else:
        print('\n未检测到【选择题】题库文件，【选择题】题库不存在')
        return 'xz_N'
def check_pd_tiku():
    if os.path.exists(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\判断题题库.docx') == True:#获取当前软件程序所在文件夹中是否有选择题库文件
        print('已检测到【判断题】题库文件，【判断题】题库存在')
        #print('位置为：' + str(os.path.dirname(os.path.realpath(sys.argv[0]))))
        return 'pd_Y'
    else:
        print('\n未检测到【判断题】题库文件，【判断题】题库不存在')
        return 'pd_N'       
def check_jd_tiku():
    if os.path.exists(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\简答题题库.docx') == True:#获取当前软件程序所在文件夹中是否有选择题库文件
        print('已检测到【简答题】题库文件，【简答题】题库存在')
        #print('位置为：' + str(os.path.dirname(os.path.realpath(sys.argv[0]))))
        return 'jd_Y'
    else:
        print('\n未检测到【简答题】题库文件，【简答题】题库不存在')
        return 'jd_N'      
def panduan_kong(ti_):#过滤掉空的回车键
    if  len(ti_)==0 :
        return False
    else :
        return True
def dangerous_box_2(time):
    top = tkinter.Tk()
    top.withdraw()
    top.update()      
    tkinter.messagebox.showinfo('成功','试卷创建成功,文件为：\n'+time+'试题卷'+'\n'+time+'答案卷')
def dangerous_box(dangerous):
    top = tkinter.Tk()
    top.withdraw()
    top.update()
    if dangerous ==1:
        tkinter.messagebox.showerror('错误','题库初始化失败，请根据提示检查【填空题题库】格式')
    if dangerous ==2:
        tkinter.messagebox.showerror('错误','题库初始化失败，请根据提示检查【选择题题库】格式')
    if dangerous ==3:
        tkinter.messagebox.showerror('错误','题库初始化失败，请根据提示检查【简答题题库】格式')
    if dangerous ==4:
        tkinter.messagebox.showinfo('成功','试卷创建成功')
    if dangerous ==5:
        tkinter.messagebox.showinfo('成功','题目添加成功')
    if dangerous ==6:
        tkinter.messagebox.showinfo('成功','题库创建成功')
    if dangerous ==7:
        tkinter.messagebox.showinfo('成功','题目删除成功')
    if dangerous ==8:
        tkinter.messagebox.showinfo('取消','已取消添加')
    if dangerous ==9:
        tkinter.messagebox.showerror('错误','题库初始化失败，请根据提示检查【判断题题库】格式')
    if dangerous ==10:
        tkinter.messagebox.showerror('错误','题干格式错误，请检查题干内是否缺少括号\n注意答案写在括号内')

##########################################################################################################################################
def creat_newpaper():#创建新试卷功能
    shijuan = Document()     
    shijuan.styles['Normal'].font.name = '仿宋'#以下两句设置正文字体
    shijuan.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'),'仿宋')#设置代码为东亚，一般跟着上面一句
    dajuan = Document()
    dajuan.styles['Normal'].font.name = '仿宋'#以下两句设置正文字体
    dajuan.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'),'仿宋')#设置代码为东亚，一般跟着上面一句
    global count, new_tihao_tk,new_tihao_xz,new_tihao_jd
    section = shijuan.sections[0]
    section_dajuan = dajuan.sections[0]
    while(True):
            set_size=input('请选择纸卷纸张大小：\n1、A4(297 X 210)   2、A3(420 X 297)   3、退出创建\n')
            if(set_size.isdigit()==False or int(set_size)>3 or int(set_size)<=0):
                print('输入错误，请重新输入，注意输入数字！')
            else:
                set_size=int(set_size)
                break
    if(set_size==1):
        section.page_height = Mm(297)#以下两句是设置纸张大小为A4
        section.page_width = Mm(210) 
        section_dajuan.page_height = Mm(297)#以下两句是设置纸张大小为A4
        section_dajuan.page_width = Mm(210) 
    elif(set_size==2):
        section.page_height = Mm(420)#以下两句是设置纸张大小为A4
        section.page_width = Mm(297) 
        section_dajuan.page_height = Mm(420)#以下两句是设置纸张大小为A4
        section_dajuan.page_width = Mm(297) 
    elif(set_size==3):
        return
    while(True):
            set_hengxiang=input('请选择试卷纸张方向：\n1、横向   2、纵向   3、退出创建\n')
            if(set_hengxiang.isdigit()==False or int(set_hengxiang)>3 or int(set_hengxiang)<=0):
                print('输入错误，请重新输入，注意输入数字！')
            else:
                set_hengxiang=int(set_hengxiang)
                break
    if(set_hengxiang==1):
        new_width,new_height = section.page_height,section.page_width#以下四句是设置纸张方向为横向
        section.orientation = WD_ORIENT.LANDSCAPE
        section.page_width = new_width
        section.page_height = new_height
        ############设置试卷标题头##########
        biaoti=shijuan.add_heading('',level = 0)#添加一个标题
        biaoti_input=str(input('请输入试卷标题：'))
        biaotiwz  = biaoti.add_run(biaoti_input)#添加标题的内容
        biaotiwz.font.name='黑体'#以下两句为专门改变标题字体和颜色的语法
        biaotiwz._element.rPr.rFonts.set(qn('w:eastAsia'), '黑体')
        biaotiwz.font.color.rgb = RGBColor(0,0,0)#改变字体颜色
        biaoti.alignment = WD_ALIGN_PARAGRAPH.CENTER#设置标题（段落）居中
        biaoti2=shijuan.add_heading('',level=3)
        biaoti2xm = biaoti2.add_run("")
        biaoti2xm.text='单位                  姓名                    分数'
        biaoti2xm.font.name='楷体'#以下两句为专门改变标题字体和颜色的语法
        biaoti2xm._element.rPr.rFonts.set(qn('w:eastAsia'), '楷体')
        section._sectPr.xpath('./w:cols')[0].set(qn('w:num'),'2')#将试卷分两栏
        print('已设置试卷方向为【横向】')
        new_width,new_height = section_dajuan.page_height,section_dajuan.page_width#以下四局是设置纸张方向为横向
        section_dajuan.orientation = WD_ORIENT.LANDSCAPE
        section_dajuan.page_width = new_width
        section_dajuan.page_height = new_height
#####################设置答卷标题头##########
        biaoti_dajuan=dajuan.add_heading('',level = 0)#添加一个标题
        biaotiwz_dajuan  = biaoti_dajuan.add_run(biaoti_input+'  (答案)')#添加标题的内容
        biaotiwz_dajuan.font.name='黑体'#以下两句为专门改变标题字体和颜色的语法
        biaotiwz_dajuan._element.rPr.rFonts.set(qn('w:eastAsia'), '黑体')
        biaotiwz_dajuan.font.color.rgb = RGBColor(0,0,0)#改变字体颜色
        biaoti_dajuan.alignment = WD_ALIGN_PARAGRAPH.CENTER#设置标题（段落）居中
        biaoti2_dajuan=dajuan.add_heading('',level=3)
        biaoti2xm_dajuan = biaoti2_dajuan.add_run("")
        biaoti2xm_dajuan.text='单位                  姓名                    分数'
        biaoti2xm_dajuan.font.name='楷体'#以下两句为专门改变标题字体和颜色的语法
        biaoti2xm_dajuan._element.rPr.rFonts.set(qn('w:eastAsia'), '楷体')
        section_dajuan._sectPr.xpath('./w:cols')[0].set(qn('w:num'),'2')#将试卷分两栏
    elif(set_hengxiang==2):
        new_width,new_height = section.page_width,section.page_height#以下四句是设置纸张方向为横向
        section.orientation = WD_ORIENT.PORTRAIT
        section.page_width = new_width
        section.page_height = new_height
        ########设置试卷标题头##########
        biaoti=shijuan.add_heading('',level = 0)#添加一个标题
        biaoti_input=str(input('请输入试卷标题：'))
        biaotiwz  = biaoti.add_run(biaoti_input)#添加标题的内容
        biaotiwz.font.name='黑体'#以下两句为专门改变标题字体和颜色的语法
        biaotiwz._element.rPr.rFonts.set(qn('w:eastAsia'), '黑体')
        biaotiwz.font.color.rgb = RGBColor(0,0,0)#改变字体颜色
        biaoti.alignment = WD_ALIGN_PARAGRAPH.CENTER#设置标题（段落）居中
        biaoti2=shijuan.add_heading('',level=3)
        biaoti2xm = biaoti2.add_run("")
        biaoti2xm.text='单位                    姓名                      分数'
        biaoti2xm.font.name='楷体'#以下两句为专门改变标题字体和颜色的语法
        biaoti2xm._element.rPr.rFonts.set(qn('w:eastAsia'), '楷体')
        print('已设置试卷方向为【纵向】')
        ########设置答卷标题头##########
        biaoti_dajuan=dajuan.add_heading('',level = 0)#添加一个标题
        biaotiwz_dajuan  = biaoti_dajuan.add_run(biaoti_input+'  (答案)')#添加标题的内容
        biaotiwz_dajuan.font.name='黑体'#以下两句为专门改变标题字体和颜色的语法
        biaotiwz_dajuan._element.rPr.rFonts.set(qn('w:eastAsia'), '黑体')
        biaotiwz_dajuan.font.color.rgb = RGBColor(0,0,0)#改变字体颜色
        biaoti_dajuan.alignment = WD_ALIGN_PARAGRAPH.CENTER#设置标题（段落）居中
        biaoti2_dajuan=dajuan.add_heading('',level=3)
        biaoti2xm_dajuan = biaoti2_dajuan.add_run("")
        biaoti2xm_dajuan.text='单位                    姓名                      分数'
        biaoti2xm_dajuan.font.name='楷体'#以下两句为专门改变标题字体和颜色的语法
        biaoti2xm_dajuan._element.rPr.rFonts.set(qn('w:eastAsia'), '楷体')
    elif(set_hengxiang==3):
        return
    def suijitihao(ti_count):#随机生成一个不重复数列，即需要抽取的题号组成一个数组，供全局使用
        i=random.randint(1,ti_count)#从获取的总题数里面随机抽一个数
        if(i in list_tihao)==True:#判断抽出来的数字已经出现没有，如果已经出现过则重新抽取
            suijitihao(ti_count)
        else:    
            list_tihao.append(i)#如果是未出现的数则将其加入数组
###########################
####自动生成填空题##########
    def choose_tiankong(datihao):
            global list_tihao,new_tihao_tk,tiankong_ok
            list_tihao=[]
            count = 0 
            tiankongti = Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\填空题题库.docx')
            for tiankong in tiankongti.paragraphs:#获取总共有多少题count
                if panduan_kong(tiankong.text)==True :
                    count=count+1
                else:
                    count = count
            print('\n当前【填空题题库】中共有 '+str(count)+' 个题\n')
            if count==0:
                print('当前题库没有可抽取的题，请在题库中添加题目后再选择...')
                return 1
            tiankongtibiaoti=shijuan.add_paragraph('')
            tiankongtibiaotiwenzi_tihao=tiankongtibiaoti.add_run(datihao)
            tiankongtibiaotiwenzi_tihao.bold = True   #加粗
            tiankongtibiaotiwenzi_tihao.font.size=Pt(15)
            tiankongtibiaotiwenzi=tiankongtibiaoti.add_run('、填空题')
            tiankongtibiaotiwenzi.bold = True   #加粗
            tiankongtibiaotiwenzi.font.size=Pt(15)
            tiankongtibiaoti_dajuan=dajuan.add_paragraph('')
            tiankongtibiaotiwenzi_dajuan_tihao=tiankongtibiaoti_dajuan.add_run(datihao)
            tiankongtibiaotiwenzi_dajuan_tihao.bold = True   #加粗
            tiankongtibiaotiwenzi_dajuan_tihao.font.size=Pt(15)
            tiankongtibiaotiwenzi_dajuan=tiankongtibiaoti_dajuan.add_run('、填空题')
            tiankongtibiaotiwenzi_dajuan.bold = True   #加粗
            tiankongtibiaotiwenzi_dajuan.font.size=Pt(15)
            while(True):
                while(True):
                        count_tishu_tk = input('请输入需要抽取入试卷的【填空题】数量\n(退出创建请直接关闭程序)\n')
                        if(count_tishu_tk.isdigit()==False):
                            print('输入错误，请重新输入，注意输入数字！')
                        else:
                            count_tishu_tk=int(count_tishu_tk)
                            break
                if(count_tishu_tk>count):
                    print('输入数量超过题库最大题数，请重新输入')
                else:
                    break
            for cishu in range(count_tishu_tk):#调用生成数列的长度,count_tishu_tk为输入的题目数量，即数列长度为想抽的题的数量
                suijitihao(count)
            list_tihao_tk=list_tihao
            for cishu in list_tihao_tk:#将题目从题库中抽取出来（根据生成的题号抽）    
                new_tihao_tk=new_tihao_tk+1
                for tiankong in tiankongti.paragraphs:#找到对应的题然后再在题库中抽取出来后改新题号再写入试卷答卷
                    if(tiankong.text[0]==str(cishu) or tiankong.text[0]+tiankong.text[1]==str(cishu)or tiankong.text[0]+tiankong.text[1]+tiankong.text[2]==str(cishu)  ):
                        if(tiankong.text[0]==str(cishu)):#替换旧题号
                            ti=tiankong.text.replace(tiankong.text[0],str(new_tihao_tk),1)
                        elif(tiankong.text[0]+tiankong.text[1]==str(cishu)):
                            ti=tiankong.text.replace(tiankong.text[0]+tiankong.text[1],str(new_tihao_tk),1)
                        elif(tiankong.text[0]+tiankong.text[1]+tiankong.text[2]==str(cishu)):
                            ti=tiankong.text.replace(tiankong.text[0]+tiankong.text[1]+tiankong.text[2],str(new_tihao_tk),1)  
                        if tiankong.text.find('(')== -1 :
                            ti_shijuan=re.sub(r"(?<=\（).+?(?=\）)","           ",ti) #正则表达式，用空白替换掉题目中的括号内内容，区分全角半角防止出错 
                        elif tiankong.text.find('（')== -1 :   
                            ti_shijuan=re.sub(r"(?<=\().+?(?=\))","           ",ti) #正则表达式，用空白替换掉题目中的括号内内容，区分全角半角防止出错
                        #print(ti_shijuan)          
                        write_tiankong=shijuan.add_paragraph('')
                        write_tiankong_wz=write_tiankong.add_run(ti_shijuan).bold
                        write_tiankong_dajuan=dajuan.add_paragraph('')
                        write_tiankong_wz_dajuan=write_tiankong_dajuan.add_run(ti).bold
                        #print(ti)#在终端将抽取到的题目显示出来
                        break
                if new_tihao_tk > count :#重新定义新题号顺序
                    new_tihao_tk=0
            print('已抽取到题库中题号为列表中的题目： ',end='')
            print (list_tihao)
            print('填空题已经抽取并写入成功，请继续\n')
            tiankong_ok=1
        ###########################

####自动生成选择题##########
    def choose_xuanze(datihao):
            global list_tihao,new_tihao_xz,xuanze_ok
            list_tihao=[]
            count = 0 
            xuanzeti = Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\选择题题库.docx')
            for xuanze in xuanzeti.paragraphs:#获取总共有多少题cunt
                if panduan_kong(xuanze.text)==True :
                    count=count+1
                else:
                    count = count
            print('\n当前【选择题题库】中共有 '+str(count)+' 个题\n')
            if count==0:
                print('当前题库没有可抽取的题，请在题库中添加题目后再选择...')
                return 1
            xuanzetibiaoti=shijuan.add_paragraph('')
            xuanzetibiaotiwenzi_tihao=xuanzetibiaoti.add_run(datihao)
            xuanzetibiaotiwenzi_tihao.bold = True   #加粗
            xuanzetibiaotiwenzi_tihao.font.size=Pt(15)
            xuanzetibiaotiwenzi=xuanzetibiaoti.add_run('、选择题')
            xuanzetibiaotiwenzi.bold = True   #加粗
            xuanzetibiaotiwenzi.font.size=Pt(15)
            xuanzetibiaoti_dajuan=dajuan.add_paragraph('')
            xuanzetibiaotiwenzi_dajuan_tihao=xuanzetibiaoti_dajuan.add_run(datihao)
            xuanzetibiaotiwenzi_dajuan_tihao.bold = True   #加粗
            xuanzetibiaotiwenzi_dajuan_tihao.font.size=Pt(15)
            xuanzetibiaotiwenzi_dajuan=xuanzetibiaoti_dajuan.add_run('、选择题')
            xuanzetibiaotiwenzi_dajuan.bold = True   #加粗
            xuanzetibiaotiwenzi_dajuan.font.size=Pt(15)
            while(True):
                while(True):
                        count_tishu_xz = input('请输入需要抽取入试卷的【选择题】数量\n(退出创建请直接关闭程序)\n')
                        if(count_tishu_xz.isdigit()==False):
                            print('输入错误，请重新输入，注意输入数字！')
                        else:
                            count_tishu_xz=int(count_tishu_xz)
                            break
                if(count_tishu_xz>count):
                    print('输入数量超过题库最大题数，请重新输入\n')
                else:
                    break
            for cishu in range(count_tishu_xz):#调用生成数列的长度，即X长度即几个题，默认count为总题数，改变count 即可改变抽取的题目数量
                suijitihao(count)
            list_tihao_xz=list_tihao
            for cishu in list_tihao_xz:#将题目从题库中抽取出来（根据生成的题号抽）    
                new_tihao_xz=new_tihao_xz+1
                for xuanze in xuanzeti.paragraphs:
                    if(xuanze.text[0]==str(cishu) or xuanze.text[0]+xuanze.text[1]==str(cishu)or xuanze.text[0]+xuanze.text[1]+xuanze.text[2]==str(cishu) ):
                        if(xuanze.text[0]==str(cishu)):#替换旧题号
                            ti=xuanze.text.replace(xuanze.text[0],str(new_tihao_xz),1)
                        elif(xuanze.text[0]+xuanze.text[1]==str(cishu)):
                            ti=xuanze.text.replace(xuanze.text[0]+xuanze.text[1],str(new_tihao_xz),1)
                        elif(xuanze.text[0]+xuanze.text[1]+xuanze.text[2]==str(cishu)):
                            ti=xuanze.text.replace(xuanze.text[0]+xuanze.text[1]+xuanze.text[2],str(new_tihao_xz),1)
                        if xuanze.text.find('(')== -1 :
                            ti_shijuan=re.sub(r"(?<=\（).+?(?=\）)","           ",ti) #正则表达式，用空白替换掉题目中的括号内内容，区分全角半角防止出错 
                        elif xuanze.text.find('（')== -1 :   
                            ti_shijuan=re.sub(r"(?<=\().+?(?=\))","           ",ti) #正则表达式，用空白替换掉题目中的括号内内容，区分全角半角防止出错
                        write_xuanze=shijuan.add_paragraph('')
                        write_xuanze_wz=write_xuanze.add_run(ti_shijuan.split('&')[0]).bold
                        xuanxiang_shijuan="".join(ti_shijuan.split('&')[1].split( ))
                        if xuanxiang_shijuan.find('B')>10 and xuanxiang_shijuan.find('C')!=-1 or len(xuanxiang_shijuan)>28:
                            write_xuanzexx_1=shijuan.add_paragraph('') 
                            write_xuanze_xxwz_1=write_xuanzexx_1.add_run(xuanxiang_shijuan[:xuanxiang_shijuan.find('B')]).bold
                            write_xuanzexx_2=shijuan.add_paragraph('') 
                            write_xuanze_xxwz_2=write_xuanzexx_2.add_run(xuanxiang_shijuan[xuanxiang_shijuan.find('B'):xuanxiang_shijuan.find('C')]).bold
                            if xuanxiang_shijuan.find('D')==-1:
                                write_xuanzexx_3=shijuan.add_paragraph('') 
                                write_xuanze_xxwz_3=write_xuanzexx_3.add_run(xuanxiang_shijuan[xuanxiang_shijuan.find('C'):]).bold
                            elif xuanxiang_shijuan.find('D')!=-1:
                                write_xuanzexx_3=shijuan.add_paragraph('') 
                                write_xuanze_xxwz_3=write_xuanzexx_3.add_run(xuanxiang_shijuan[xuanxiang_shijuan.find('C'):xuanxiang_shijuan.find('D')]).bold
                                write_xuanzexx_4=shijuan.add_paragraph('') 
                                write_xuanze_xxwz_4=write_xuanzexx_4.add_run(xuanxiang_shijuan[xuanxiang_shijuan.find('D'):]).bold
                        else:
                            write_xuanzexx=shijuan.add_paragraph('')
                            write_xuanze_xxwz=write_xuanzexx.add_run(ti_shijuan.split('&')[1]).bold
                        write_xuanze_dajuan=dajuan.add_paragraph('')
                        write_xuanze_wz_dajuan=write_xuanze_dajuan.add_run(ti.split('&')[0]).bold
                        xuanxiang_dajuan="".join(ti.split('&')[1].split( ))
                        #print(xuanxiang_dajuan.find('B'))
                        if xuanxiang_dajuan.find('B')>10 and xuanxiang_dajuan.find('C')!=-1 or len(xuanxiang_dajuan)>28:
                            write_xuanzexx_1da=dajuan.add_paragraph('') 
                            write_xuanze_xxwz_1da=write_xuanzexx_1da.add_run(xuanxiang_dajuan[:xuanxiang_dajuan.find('B')]).bold
                            write_xuanzexx_2da=dajuan.add_paragraph('') 
                            write_xuanze_xxwz_2da=write_xuanzexx_2da.add_run(xuanxiang_dajuan[xuanxiang_dajuan.find('B'):xuanxiang_dajuan.find('C')]).bold
                            if xuanxiang_dajuan.find('D')==-1:
                                write_xuanzexx_3da=dajuan.add_paragraph('') 
                                write_xuanze_xxwz_3da=write_xuanzexx_3da.add_run(xuanxiang_dajuan[xuanxiang_dajuan.find('C'):]).bold
                            elif xuanxiang_dajuan.find('D')!=-1:
                                write_xuanzexx_3da=dajuan.add_paragraph('') 
                                write_xuanze_xxwz_3da=write_xuanzexx_3da.add_run(xuanxiang_dajuan[xuanxiang_dajuan.find('C'):xuanxiang_dajuan.find('D')]).bold
                                write_xuanzexx_4da=dajuan.add_paragraph('') 
                                write_xuanze_xxwz_4da=write_xuanzexx_4da.add_run(xuanxiang_dajuan[xuanxiang_dajuan.find('D'):]).bold
                        else:
                            write_xuanzexx_dajuan=dajuan.add_paragraph('')
                            write_xuanze_xxwz_dajuan=write_xuanzexx_dajuan.add_run(ti.split('&')[1]).bold
                        #print(ti.split&')[0])#在终端将抽取到的题目显示出来
                        break
                if new_tihao_xz > count :#重新定义新题号顺序
                    new_tihao_xz=0
            print('已抽取到题库中题号为列表中的题目： ',end='')
            print (list_tihao)
            print('选择题已经抽取并写入成功，请继续\n')
            xuanze_ok=1
###########################
####自动生成判断题##########
    def choose_panduan(datihao):
            global list_tihao,new_tihao_pd,tiankong_ok
            list_tihao=[]
            count = 0 
            panduanti = Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\判断题题库.docx')
            for panduan in panduanti.paragraphs:#获取总共有多少题count
                if panduan_kong(panduan.text)==True :
                    count=count+1
                else:
                    count = count
            print('\n当前【判断题题库】中共有 '+str(count)+' 个题\n')
            if count==0:
                print('当前题库没有可抽取的题，请在题库中添加题目后再选择...')
                return 1
            panduantibiaoti=shijuan.add_paragraph('')
            panduantibiaotiwenzi_tihao=panduantibiaoti.add_run(datihao)
            panduantibiaotiwenzi_tihao.bold = True   #加粗
            panduantibiaotiwenzi_tihao.font.size=Pt(15)
            panduantibiaotiwenzi=panduantibiaoti.add_run('、判断题')
            panduantibiaotiwenzi.bold = True   #加粗
            panduantibiaotiwenzi.font.size=Pt(15)
            panduantibiaoti_dajuan=dajuan.add_paragraph('')
            panduantibiaotiwenzi_dajuan_tihao=panduantibiaoti_dajuan.add_run(datihao)
            panduantibiaotiwenzi_dajuan_tihao.bold = True   #加粗
            panduantibiaotiwenzi_dajuan_tihao.font.size=Pt(15)
            panduantibiaotiwenzi_dajuan=panduantibiaoti_dajuan.add_run('、判断题')
            panduantibiaotiwenzi_dajuan.bold = True   #加粗
            panduantibiaotiwenzi_dajuan.font.size=Pt(15)
            while(True):
                while(True):
                        count_tishu_tk = input('请输入需要抽取入试卷的【判断题】数量\n(退出创建请直接关闭程序)\n')
                        if(count_tishu_tk.isdigit()==False):
                            print('输入错误，请重新输入，注意输入数字！')
                        else:
                            count_tishu_tk=int(count_tishu_tk)
                            break
                if(count_tishu_tk>count):
                    print('输入数量超过题库最大题数，请重新输入')
                else:
                    break
            for cishu in range(count_tishu_tk):#调用生成数列的长度，即X长度即几个题，默认count为总题数，改变count 即可改变抽取的题目数量
                suijitihao(count)
            list_tihao_pd=list_tihao
            for cishu in list_tihao_pd:#将题目从题库中抽取出来（根据生成的题号抽）    
                new_tihao_pd=new_tihao_pd+1
                for panduan in panduanti.paragraphs:
                    if(panduan.text[0]==str(cishu) or panduan.text[0]+panduan.text[1]==str(cishu)or panduan.text[0]+panduan.text[1]+panduan.text[2]==str(cishu)  ):
                        if(panduan.text[0]==str(cishu)):#替换旧题号
                            ti=panduan.text.replace(panduan.text[0],str(new_tihao_pd),1)
                        elif(panduan.text[0]+panduan.text[1]==str(cishu)):
                            ti=panduan.text.replace(panduan.text[0]+panduan.text[1],str(new_tihao_pd),1)
                        elif(panduan.text[0]+panduan.text[1]+panduan.text[2]==str(cishu)):
                            ti=panduan.text.replace(panduan.text[0]+panduan.text[1]+panduan.text[2],str(new_tihao_pd),1)  
                        if panduan.text.find('(')== -1 :
                            ti_shijuan=re.sub(r"(?<=\（).+?(?=\）)","       ",ti) #正则表达式，用空白替换掉题目中的括号内内容，区分全角半角防止出错
                        elif tiankong.text.find('（')== -1 :   
                            ti_shijuan=re.sub(r"(?<=\().+?(?=\))","       ",ti) #正则表达式，用空白替换掉题目中的括号内内容，区分全角半角防止出错
                        #print(ti_shijuan)          
                        write_panduan=shijuan.add_paragraph('')
                        write_panduan_wz=write_panduan.add_run(ti_shijuan).bold
                        write_panduan_dajuan=dajuan.add_paragraph('')
                        write_panduan_wz_dajuan=write_panduan_dajuan.add_run(ti).bold
                        #print(ti)#在终端将抽取到的题目显示出来
                        break
                if new_tihao_pd > count :#重新定义新题号顺序
                    new_tihao_pd=0
            print('已抽取到题库中题号为列表中的题目： ',end='')
            print (list_tihao)
            print('判断题已经抽取并写入成功，请继续\n')
            panduan_ok=1
####自动生成简答题##########
    def choose_jianda(datihao):
            global list_tihao,new_tihao_jd,jianda_ok
            list_tihao=[]
            count = 0 
            jiandati = Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\简答题题库.docx')
            for jianda in jiandati.paragraphs:#获取总共有多少题cunt
                if panduan_kong(jianda.text)==True :
                    count=count+1
                else:
                    count = count
            print('\n当前【简答题题库】中共有 '+str(count)+' 个题\n')
            if count==0:
                print('当前题库没有可抽取的题，请在题库中添加题目后再选择...')
                return 1
            jiandatibiaoti=shijuan.add_paragraph('')
            jiandatibiaotiwenzi_tihao=jiandatibiaoti.add_run(datihao)
            jiandatibiaotiwenzi_tihao.bold = True   #加粗
            jiandatibiaotiwenzi_tihao.font.size=Pt(15)
            jiandatibiaotiwenzi=jiandatibiaoti.add_run('、简答题')
            jiandatibiaotiwenzi.bold = True   #加粗
            jiandatibiaotiwenzi.font.size=Pt(15)
            jiandatibiaoti_dajuan=dajuan.add_paragraph('')
            jiandatibiaotiwenzi_dajuan_tihao=jiandatibiaoti_dajuan.add_run(datihao)
            jiandatibiaotiwenzi_dajuan_tihao.bold = True   #加粗
            jiandatibiaotiwenzi_dajuan_tihao.font.size=Pt(15)
            jiandatibiaotiwenzi_dajuan=jiandatibiaoti_dajuan.add_run('、简答题')
            jiandatibiaotiwenzi_dajuan.bold = True   #加粗
            jiandatibiaotiwenzi_dajuan.font.size=Pt(15)
            while(True):
                while(True):
                        count_tishu_jd = input('请输入需要抽取入试卷的【简答题】数量\n(退出创建请直接关闭程序)\n')
                        if(count_tishu_jd.isdigit()==False):
                            print('输入错误，请重新输入，注意输入数字！')
                        else:
                            count_tishu_jd=int(count_tishu_jd)
                            break
                if(count_tishu_jd>count):
                    print('输入数量超过题库最大题数，请重新输入\n')
                else:
                    break
            for cishu in range(count_tishu_jd):#调用生成数列的长度，即X长度即几个题，默认count为总题数，改变count 即可改变抽取的题目数量
                suijitihao(count)
            list_tihao_jd=list_tihao
            for cishu in list_tihao_jd:#将题目从题库中抽取出来（根据生成的题号抽）    
                new_tihao_jd=new_tihao_jd+1
                for jianda in jiandati.paragraphs:
                    if(jianda.text[0]==str(cishu) or jianda.text[0]+jianda.text[1]==str(cishu) or jianda.text[0]+jianda.text[1]+jianda.text[2]==str(cishu) ):
                        if(jianda.text[0]==str(cishu)):#替换旧题号
                            ti=jianda.text.replace(jianda.text[0],str(new_tihao_jd),1)
                        elif(jianda.text[0]+jianda.text[1]==str(cishu)):
                            ti=jianda.text.replace(jianda.text[0]+jianda.text[1],str(new_tihao_jd),1)
                        elif(jianda.text[0]+jianda.text[1]+jianda.text[2]==str(cishu)):
                            ti=jianda.text.replace(jiandae.text[0]+jianda.text[1]+jianda.text[2],str(new_tihao_jdz),1)
                        write_jianda=shijuan.add_paragraph('')
                        write_jianda_wz=write_jianda.add_run(ti.split('&')[0]).bold
                        if len(ti.split('&')[1])>30:
                            write_jianda_da=shijuan.add_paragraph('')
                            write_jianda_dawz=write_jianda_da.add_run('\n').bold
                            write_jianda_dawz=write_jianda_da.add_run('\n').bold
                            write_jianda_dawz=write_jianda_da.add_run('\n').bold
                        else :
                            write_jianda_da=shijuan.add_paragraph('')
                            write_jianda_dawz=write_jianda_da.add_run('\n').bold

                        write_jianda_dajuan=dajuan.add_paragraph('')
                        write_jianda_wz_dajuan=write_jianda_dajuan.add_run(ti.split('&')[0]).bold
                        write_jiandada_dajuan=dajuan.add_paragraph('')
                        write_jiandada_wz_dajuan=write_jiandada_dajuan.add_run(ti.split('&')[1]).bold
                        #print(ti.split('&')[0])#在终端将抽取到的题目显示出来
                        break
                if new_tihao_jd > count :#重新定义新题号顺序
                    new_tihao_jd=0
            print('已抽取到题库中题号为列表中的题目： ',end='')
            print (list_tihao)
            print('简答题已经抽取并写入成功，请继续\n')
            jianda_ok=1
##########选择题型及顺序###############
    def choose_set():
        global choose_count
        list_choose=['一','二','三','四','五']
        print('\n最多可以在一张试卷中添加【四个大题】')
        while True:
            if(choose_count>=4):
                print('已达到最大大题数量，将生成试卷...\n')
                return
            while True:
                choose=input('\n请选择第【'+list_choose[choose_count]+'】大题题型：1、填空题   2、选择题   3、判断题   4、简答题    5、生成试卷\n')
                if len(str(choose))==0:
                    pass
                else:
                    choose=int(choose)
                    break
            
            if(choose==1):
                if check_tk_tiku()=='tk_Y':
                    if(choose_tiankong(list_choose[choose_count])!=1):
                        choose_count=choose_count+1
                    else:
                        choose_count=choose_count
                else:
                    choose_count=choose_count
            elif (choose==2):
                if check_xz_tiku()=='xz_Y':
                    if(choose_xuanze(list_choose[choose_count])!=1):
                        choose_count=choose_count+1
                    else:
                        choose_count=choose_count
                else:
                    choose_count=choose_count
            elif(choose==3):
                if check_pd_tiku()=='pd_Y':
                    if(choose_panduan(list_choose[choose_count])!=1):
                        choose_count=choose_count+1
                    else:
                        choose_count=choose_count
                else:
                    choose_count=choose_count               
            elif(choose==4):
                if check_jd_tiku()=='jd_Y':
                    if(choose_jianda(list_choose[choose_count])!=1):
                        choose_count=choose_count+1
                    else:
                        choose_count=choose_count
                else:
                    choose_count=choose_count        
            elif(choose==5):
                if choose_count>=1:
                    print('选择完成，试卷正在生成...\n')
                    choose_count=0
                    return
                else:
                    print('已取消生成试卷...')
                    choose_count=0
                    return
            

###########创建新试卷成功##########
    choose_set()
    global tiankong_ok,xuanze_ok,jianda_ok,panduan_ok
    global new_tihao_tk ,new_tihao_xz , new_tihao_pd , new_tihao_jd
    new_tihao_tk=0
    new_tihao_xz=0
    new_tihao_pd=0
    new_tihao_jd=0
    if(tiankong_ok==1 or xuanze_ok==1 or panduan_ok==1 or jianda_ok==1):
        now_time = datetime.datetime.now()
        time_str=now_time.strftime('%Y%m%d%H%M%S')#获取当前时间并转换为字符串
        shijuan.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\'+time_str+'试题卷.docx')
        dajuan.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\'+time_str+'答案卷.docx')   
        print('新的试卷已经创建成功')
        print('按任意键继续')
        dangerous_box_2(time_str)
        os.system('pause')
        if(sys.platform=='win32'):#判断使用者的系统是windows 还是 Linux
            os.system("cls")
            homepage()
        elif(sys.platform=='linux'):
            os.system('clear')
            homepage()
        return
############################################################################################################################################

####################################################################
def add_q_bank():
    #############添加和删除题库内容######################################

    def add_q(tiku_type):        
        set_count=1
        cancel=True
        tianjia_count = 0
        if(tiku_type==1):
            tianjia_count=0
            while(cancel):
                for tiankong in setup_tiankong_tiku.paragraphs:#获取总共有多少题count
                    if panduan_kong(tiankong.text)==True :
                        set_count=set_count+1
                    else:
                        set_count = set_count
                add_tk=tkinter.Tk()
                add_tk.withdraw()
                add_tk.update()
                add_new_ti=askstring(title='添加新的填空题',prompt='\n                          请输入新的填空题（不写题号，答案两侧加括号）：                           ：\n')
                if((add_new_ti)!=None):
                    if(add_new_ti.find('(')== -1 and add_new_ti.find('（')== -1 and add_new_ti.find(')')== -1 and add_new_ti.find('）')== -1):
                        dangerous_box(10)
                        set_count=1
                    elif(add_new_ti.count('(')== 0 and add_new_ti.count('（')== 0) or (add_new_ti.count(')')== 0 and add_new_ti.count('）')== 0):
                        dangerous_box(10)
                        set_count=1
                    else:
                        new_ti=setup_tiankong_tiku.add_paragraph()
                        new_ti.add_run(str(set_count)+'、')
                        new_ti.add_run(add_new_ti)
                        setup_tiankong_tiku.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\填空题题库.docx')
                        print('\n添加：'+str(set_count)+'、'+add_new_ti+'【成功】\n')
                        set_count=1
                        tianjia_count=tianjia_count+1
                if(add_new_ti==None and tianjia_count>0):
                    dangerous_box(5)
                    break
                elif(add_new_ti==None and tianjia_count==0):
                    dangerous_box(8)
                    break

        elif(tiku_type==2):
            tianjia_count=0
            while(cancel):
                for xuanze in setup_xuanze_tiku.paragraphs:#获取总共有多少题count
                    if panduan_kong(xuanze.text)==True :
                        set_count=set_count+1
                    else:
                        set_count = set_count
                add_tk=tkinter.Tk()
                add_tk.withdraw()
                add_tk.update()
                add_new_ti=askstring(title='添加新的选择题',prompt='\n                          请输入新的选择题（不写题号，键入括号内并写入答案，不写选项）：                           \n')
                if((add_new_ti)!=None):
                    if add_new_ti.find('(')== -1 and add_new_ti.find('（')== -1 and add_new_ti.find(')')== -1 and add_new_ti.find('）')== -1:
                        dangerous_box(10)
                        set_count=1
                    elif (add_new_ti.count('(')== 0 and add_new_ti.count('（')== 0) or (add_new_ti.count(')')== 0 and add_new_ti.count('）')== 0):
                        dangerous_box(10)
                        set_count=1
                    else:
                        add_new_ti2=askstring(title='添加对应选项',prompt='\n                          请输入题目对应选项（每个选项之间键入三个空格）                          ：\n')
                        if add_new_ti2 !=None:
                            new_ti=setup_xuanze_tiku.add_paragraph()
                            new_ti.add_run(str(set_count)+'、')
                            new_ti.add_run(add_new_ti)
                            new_ti.add_run('&')        
                            new_ti.add_run(add_new_ti2)
                            setup_xuanze_tiku.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\选择题题库.docx')
                            print('\n添加：'+str(set_count)+'、'+add_new_ti+add_new_ti2+'【成功】\n')
                            tianjia_count=tianjia_count+1
                            set_count=1
                        if(add_new_ti==None or add_new_ti2==None)and tianjia_count>0:
                            dangerous_box(5)
                            break
                        elif(add_new_ti==None or add_new_ti2==None)and tianjia_count==0:
                            dangerous_box(8)
                            break
                if(add_new_ti==None)and tianjia_count>0:
                        dangerous_box(5)
                        break
                elif(add_new_ti==None)and tianjia_count==0:
                        dangerous_box(8)
                        break
        if(tiku_type==3):
            tianjia_count=0
            while(cancel):
                for panduan in setup_panduan_tiku.paragraphs:#获取总共有多少题count
                    if panduan_kong(panduan.text)==True :
                        set_count=set_count+1
                    else:
                        set_count = set_count
                add_tk=tkinter.Tk()
                add_tk.withdraw()
                add_tk.update()
                add_new_ti=askstring(title='添加新的判断题',prompt='\n                          请输入新的判断题（不写题号，答案两侧加括号）：                           ：\n')
                if((add_new_ti)!=None):
                    if(add_new_ti.find('(')== -1 and add_new_ti.find('（')== -1 and add_new_ti.find(')')== -1 and add_new_ti.find('）')== -1):
                        dangerous_box(10)
                        set_count=1
                    elif(add_new_ti.count('(')== 0 and add_new_ti.count('（')== 0) or (add_new_ti.count(')')== 0 and add_new_ti.count('）')== 0):
                        dangerous_box(10)
                        set_count=1
                    else:
                        new_ti=setup_panduan_tiku.add_paragraph()
                        new_ti.add_run(str(set_count)+'、')
                        new_ti.add_run(add_new_ti)
                        setup_panduan_tiku.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\判断题题库.docx')
                        print('\n添加：'+str(set_count)+'、'+add_new_ti+'【成功】\n')
                        tianjia_count=tianjia_count+1
                        set_count=1
                if(add_new_ti==None and tianjia_count>0):
                    dangerous_box(5)
                    break
                elif(add_new_ti==None and tianjia_count==0):
                    dangerous_box(8)
                    break
        elif(tiku_type==4):
            tianjia_count=0
            while(cancel):
                for jianda in setup_jianda_tiku.paragraphs:#获取总共有多少题count
                    if panduan_kong(jianda.text)==True :
                        set_count=set_count+1
                    else:
                        set_count = set_count
                add_tk=tkinter.Tk()
                add_tk.withdraw()
                add_tk.update()
                add_new_ti=askstring(title='添加新的简答题',prompt='\n                          请输入新的简答题（不写题号，不写答案）：                           \n')
                if((add_new_ti)!=None):
                    new_ti=setup_jianda_tiku.add_paragraph()
                    new_ti.add_run(str(set_count)+'、')
                    new_ti.add_run(add_new_ti)
                    new_ti.add_run('&')
                    add_new_ti=askstring(title='添加对应答案',prompt='\n                          请输入题目对应答案                          ：\n')
                    new_ti.add_run(add_new_ti)
                    setup_jianda_tiku.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\简答题题库.docx')
                    print('\n添加：'+str(set_count)+'、'+add_new_ti+'【成功】\n')
                    tianjia_count=tianjia_count+1
                    set_count=1
                if(add_new_ti==None and tianjia_count>0):
                    dangerous_box(5)
                    break
                elif(add_new_ti==None and tianjia_count==0):
                    dangerous_box(8)
                    break
    
    def display(type_):
        display_count=0
        if(type_==1):
            if(check_tk_tiku()=='tk_N'):
                print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
                print('按任意键继续...')
                os.system('pause')
                return
            else:
                display_ti=Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\填空题题库.docx')
                for tiankong in display_ti.paragraphs:
                    if(len(tiankong.text)!=0):
                        display_count=display_count+1
                    else:
                        display_count=display_count
                print('一共有：'+str(display_count)+'道【填空题】\n')
                for tiankong in  display_ti.paragraphs:
                    if(len(tiankong.text)!=0):
                        print(tiankong.text)
        elif(type_==2):
            if(check_xz_tiku()=='xz_N'):
                print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
                print('按任意键继续...')
                os.system('pause')
                return
            else:
                display_ti=Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\选择题题库.docx')  
                for xuanze in  display_ti.paragraphs:
                    if(len(xuanze.text)!=0):
                        display_count=display_count+1
                    else:
                        display_count=display_count
                print('一共有：'+str(display_count)+'道【选择题】\n')
                for xuanze in  display_ti.paragraphs:
                    if(len(xuanze.text)!=0):
                        print(xuanze.text)
        if(type_==3):
            if(check_pd_tiku()=='pd_N'):
                print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
                print('按任意键继续...')
                os.system('pause')
                return
            else:
                display_ti=Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\判断题题库.docx')
                for panduan in display_ti.paragraphs:
                    if(len(panduan.text)!=0):
                        display_count=display_count+1
                    else:
                        display_count=display_count
                print('一共有：'+str(display_count)+'道【判断题】\n')
                for panduan in  display_ti.paragraphs:
                    if(len(panduan.text)!=0):
                        print(panduan.text)
        elif(type_==4):
            if(check_jd_tiku()=='jd_N'):
                print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
                print('按任意键继续...')
                os.system('pause')
                return
            else:
                display_ti=Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\简答题题库.docx') 
                for jianda in  display_ti.paragraphs:
                    if(len(jianda.text)!=0):
                        display_count=display_count+1
                    else:
                        display_count=display_count
                print('一共有：'+str(display_count)+'道【简答题】\n')
                for jianda in  display_ti.paragraphs:
                    if(len(jianda.text)!=0):
                        print(jianda.text) 
    def add_new_ti(type_): 
        new_count=1
        cancel = True
        tianjia_count=0
        if(type_==1):
            tianjia_count=0
            if(check_tk_tiku()=='tk_N'):
                print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
                print('按任意键继续...')
                os.system('pause')
                return
            else:
                add_ti=Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\填空题题库.docx')
                while(cancel):
                    for tiankong in add_ti.paragraphs:  #获取总共有多少题count
                        if panduan_kong(tiankong.text)==True :
                            new_count=new_count+1
                        else:
                            new_count = new_count
                    add_tk=tkinter.Tk()
                    add_tk.withdraw()
                    add_tk.update()
                    add_new_ti=askstring(title='添加新的填空题',prompt='\n                          请输入新的填空题（不写题号，答案两侧加括号）：                           ：\n')
                    if((add_new_ti)!=None):
                        if(add_new_ti.find('(')== -1 and add_new_ti.find('（')== -1 and add_new_ti.find(')')== -1 and add_new_ti.find('）')== -1):
                            dangerous_box(10)
                            new_count=1
                        elif(add_new_ti.count('(')== 0 and add_new_ti.count('（')== 0) or (add_new_ti.count(')')== 0 and add_new_ti.count('）')== 0):
                            dangerous_box(10)
                            new_count=1
                        else:
                            new_ti=add_ti.add_paragraph()
                            new_ti.add_run(str(new_count)+'、')
                            new_ti.add_run(add_new_ti)
                            add_ti.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\填空题题库.docx')
                            print('\n添加：'+str(new_count)+'、'+add_new_ti+'【成功】\n')
                            new_count=1
                            tianjia_count=tianjia_count+1
                    if(add_new_ti==None and tianjia_count>0):
                        dangerous_box(5)
                        break
                    elif add_new_ti==None and tianjia_count==0:
                        dangerous_box(8)
                        break
        if(type_==2):
            tianjia_count=0
            if(check_xz_tiku()=='xz_N'):
                print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
                print('按任意键继续...')
                os.system('pause')
                return
            else:
                add_ti=Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\选择题题库.docx')
                while(cancel):
                    for xuanze in add_ti.paragraphs:  #获取总共有多少题count
                        if panduan_kong(xuanze.text)==True :
                            new_count=new_count+1
                        else:
                            new_count = new_count
                    add_tk=tkinter.Tk()
                    add_tk.withdraw()
                    add_tk.update()
                    add_new_ti=askstring(title='添加新的选择题',prompt='\n                          请输入新的选择题（不写题号，键入括号内并写入答案，不写选项）：                           \n')
                    if((add_new_ti)!=None):
                        if add_new_ti.find('(')== -1 and add_new_ti.find('（')== -1 and add_new_ti.find(')')== -1 and add_new_ti.find('）')== -1:
                            dangerous_box(10)
                            new_count=1
                        elif (add_new_ti.count('(')== 0 and add_new_ti.count('（')== 0) or (add_new_ti.count(')')== 0 and add_new_ti.count('）')== 0):
                            dangerous_box(10)
                            new_count=1
                        else:
                            add_new_ti2=askstring(title='添加对应选项',prompt='\n                          请输入题目对应选项（每个选项之间键入三个空格）                          ：\n')
                            if add_new_ti2 !=None:
                                new_ti=add_ti.add_paragraph()
                                new_ti.add_run(str(new_count)+'、')
                                new_ti.add_run(add_new_ti)
                                new_ti.add_run('&')
                                new_ti.add_run(add_new_ti2)
                                add_ti.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\选择题题库.docx')
                                print('\n添加：'+str(new_count)+'、'+add_new_ti+add_new_ti2+'【成功】\n')
                                new_count=1
                                tianjia_count=tianjia_count+1
                            if((add_new_ti==None or add_new_ti2==None)and tianjia_count>0):
                                dangerous_box(5)
                                break
                            elif ((add_new_ti==None or add_new_ti2==None)and tianjia_count==0):
                                dangerous_box(8)
                                break
                    if((add_new_ti==None or add_new_ti2==None)and tianjia_count>0):
                            dangerous_box(5)
                            break
                    elif ((add_new_ti==None or add_new_ti2==None)and tianjia_count==0):
                            dangerous_box(8)
                            break
                  
        if(type_==3):
            tianjia_count=0
            if(check_pd_tiku()=='pd_N'):
                print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
                print('按任意键继续...')
                os.system('pause')
                return
            else:
                add_ti=Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\判断题题库.docx')
                while(cancel):
                    for panduan in add_ti.paragraphs:  #获取总共有多少题count
                        if panduan_kong(panduan.text)==True :
                            new_count=new_count+1
                        else:
                            new_count = new_count
                    add_tk=tkinter.Tk()
                    add_tk.withdraw()
                    add_tk.update()
                    add_new_ti=askstring(title='添加新的判断题',prompt='\n                          请输入新的判断题（不写题号，答案两侧加括号）：                           ：\n')
                    if((add_new_ti)!=None):
                        if(add_new_ti.find('(')== -1 and add_new_ti.find('（')== -1 and add_new_ti.find(')')== -1 and add_new_ti.find('）')== -1):
                            dangerous_box(10)
                            new_count=1
                        elif(add_new_ti.count('(')== 0 and add_new_ti.count('（')== 0) or (add_new_ti.count(')')== 0 and add_new_ti.count('）')== 0):
                            dangerous_box(10)
                            new_count=1
                        else:
                            new_ti=add_ti.add_paragraph()
                            new_ti.add_run(str(new_count)+'、')
                            new_ti.add_run(add_new_ti)
                            add_ti.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\判断题题库.docx')
                            print('\n添加：'+str(new_count)+'、'+add_new_ti+'【成功】\n')
                            new_count=1
                            tianjia_count=tianjia_count+1
                    if(add_new_ti==None and tianjia_count>0):
                        dangerous_box(5)
                        break
                    elif add_new_ti==None and tianjia_count==0:
                        dangerous_box(8)
                        break
        if(type_==4):  
            tianjia_count=0  
            if(check_jd_tiku()=='jd_N'):
                print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
                print('按任意键继续...')
                os.system('pause')
                return
            else:     
                add_ti=Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\简答题题库.docx')
                while(cancel):
                    for jianda in add_ti.paragraphs:  #获取总共有多少题count
                        if panduan_kong(jianda.text)==True :
                            new_count=new_count+1
                        else:
                            new_count = new_count
                    add_tk=tkinter.Tk()
                    add_tk.withdraw()
                    add_tk.update()
                    add_new_ti=askstring(title='添加新的简答题',prompt='\n                          请输入新的简答题（不写题号，不写答案）：                           \n')
                    if((add_new_ti)!=None):
                        new_ti=add_ti.add_paragraph()
                        new_ti.add_run(str(new_count)+'、')
                        new_ti.add_run(add_new_ti)
                        new_ti.add_run('&')
                        add_new_ti=askstring(title='添加对应答案',prompt='\n                          请输入题目对应答案                          ：\n')
                        new_ti.add_run(add_new_ti)
                        add_ti.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\简答题题库.docx')
                        print('\n添加：'+str(new_count)+'、'+add_new_ti+'【成功】\n')
                        new_count=1
                        tianjia_count=tianjia_count+1
                    if(add_new_ti==None and tianjia_count>0):
                        dangerous_box(5)
                        break
                    elif add_new_ti==None and tianjia_count==0:
                        dangerous_box(8)
                        break

    def delete_q(type_):
        new_count=0
        cancel = True
        list_del=[]
        new_list_del=[]
        new_list_del2=[]
        tihao=1
        if(type_==1):
            if(check_tk_tiku()=='tk_N'):
                print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
                print('按任意键继续...')
                os.system('pause')
                return
            else:
                while(cancel):
                    del_ti=Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\填空题题库.docx')
                    for tiankong in del_ti.paragraphs:  #获取总共有多少题count
                        if panduan_kong(tiankong.text)==True :
                            new_count=new_count+1
                        else:
                            new_count = new_count
                    delete_num=input('一共有'+str(new_count)+'题，请输入需要删除的题号，注意一次只能删除一题，退出请直接按回车：\n')
                    if(len(str(delete_num))!=0):
                        for ti_ in del_ti.paragraphs:
                            list_del.append(ti_.text)
                        list_del.pop(int(delete_num)-1)
                        for i in list_del:
                            if len(i)!=0:
                                new_list_del.append(i)
                        new_del_ti=Document()
                        for new_ti_ in new_list_del:
                            new=new_del_ti.add_paragraph(new_ti_)
                        print('删除成功')
                        new_del_ti.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\填空题题库.docx')
                        for x in new_del_ti.paragraphs:
                            if x.text[0].isdigit()==True and x.text[1].isdigit()==False and x.text[2].isdigit()==False:
                                new_ti_hao=x.text.replace(x.text[0],str(tihao),1)
                            if x.text[0].isdigit()==True and x.text[1].isdigit()==True and x.text[2].isdigit()==False:
                                new_ti_hao=x.text.replace(x.text[0]+x.text[1],str(tihao),1)
                            if x.text[0].isdigit()==True and x.text[1].isdigit()==True and x.text[2].isdigit()==True:
                                new_ti_hao=x.text.replace(x.text[0]+x.text[1]+x.text[2],str(tihao),1)
                            new_list_del2.append(new_ti_hao)
                            tihao=tihao+1
                        new_del_ti2=Document()
                        for new_ti_ in new_list_del2:
                            new=new_del_ti2.add_paragraph(new_ti_)
                        new_del_ti2.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\填空题题库.docx')                        
                    else:
                        dangerous_box(7)
                        break
                   
                    new_count=0   
                    list_del=[]
                    new_list_del=[]
                    new_list_del2=[]
                    tihao=1
                    

        if(type_==2):
            if(check_xz_tiku()=='xz_N'):
                print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
                print('按任意键继续...')
                os.system('pause')
                return
            else:
                while(cancel):
                    del_ti=Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\选择题题库.docx')
                    for xuanze in del_ti.paragraphs:  #获取总共有多少题count
                        if panduan_kong(xuanze.text)==True :
                            new_count=new_count+1
                        else:
                            new_count = new_count
                    delete_num=input('一共有'+str(new_count)+'题，请输入需要删除的题号，注意一次只能删除一题，退出请直接按回车：\n')
                    if(len(str(delete_num))!=0):
                        for ti_ in del_ti.paragraphs:
                            list_del.append(ti_.text)
                        list_del.pop(int(delete_num)-1)
                        for i in list_del:
                            if len(i)!=0:
                                new_list_del.append(i)
                        new_del_ti=Document()
                        for new_ti_ in new_list_del:
                            new=new_del_ti.add_paragraph(new_ti_)
                        print('删除成功')
                        new_del_ti.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\选择题题库.docx')
                        for x in new_del_ti.paragraphs:
                            if x.text[0].isdigit()==True and x.text[1].isdigit()==False and x.text[2].isdigit()==False:
                                new_ti_hao=x.text.replace(x.text[0],str(tihao),1)
                            if x.text[0].isdigit()==True and x.text[1].isdigit()==True and x.text[2].isdigit()==False:
                                new_ti_hao=x.text.replace(x.text[0]+x.text[1],str(tihao),1)
                            if x.text[0].isdigit()==True and x.text[1].isdigit()==True and x.text[2].isdigit()==True:
                                new_ti_hao=x.text.replace(x.text[0]+x.text[1]+x.text[2],str(tihao),1)
                            new_list_del2.append(new_ti_hao)
                            tihao=tihao+1
                        new_del_ti2=Document()
                        for new_ti_ in new_list_del2:
                            new=new_del_ti2.add_paragraph(new_ti_)
                        new_del_ti2.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\选择题题库.docx')                        
                    else:
                        dangerous_box(7)
                        break
                      
                    new_count=0   
                    list_del=[]
                    new_list_del=[]
                    new_list_del2=[]
                    tihao=1

        if(type_==3):
            if(check_pd_tiku()=='pd_N'):
                print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
                print('按任意键继续...')
                os.system('pause')
                return
            else:
                while(cancel):
                    del_ti=Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\判断题题库.docx')
                    for panduan in del_ti.paragraphs:  #获取总共有多少题count
                        if panduan_kong(panduan.text)==True :
                            new_count=new_count+1
                        else:
                            new_count = new_count
                    delete_num=input('一共有'+str(new_count)+'题，请输入需要删除的题号，注意一次只能删除一题，退出请直接按回车：\n')
                    if(len(str(delete_num))!=0):
                        for ti_ in del_ti.paragraphs:
                            list_del.append(ti_.text)
                        list_del.pop(int(delete_num)-1)
                        for i in list_del:
                            if len(i)!=0:
                                new_list_del.append(i)
                        new_del_ti=Document()
                        for new_ti_ in new_list_del:
                            new=new_del_ti.add_paragraph(new_ti_)
                        print('删除成功')
                        new_del_ti.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\判断题题库.docx')
                        for x in new_del_ti.paragraphs:
                            if x.text[0].isdigit()==True and x.text[1].isdigit()==False and x.text[2].isdigit()==False:
                                new_ti_hao=x.text.replace(x.text[0],str(tihao),1)
                            if x.text[0].isdigit()==True and x.text[1].isdigit()==True and x.text[2].isdigit()==False:
                                new_ti_hao=x.text.replace(x.text[0]+x.text[1],str(tihao),1)
                            if x.text[0].isdigit()==True and x.text[1].isdigit()==True and x.text[2].isdigit()==True:
                                new_ti_hao=x.text.replace(x.text[0]+x.text[1]+x.text[2],str(tihao),1)
                            new_list_del2.append(new_ti_hao)
                            tihao=tihao+1
                        new_del_ti2=Document()
                        for new_ti_ in new_list_del2:
                            new=new_del_ti2.add_paragraph(new_ti_)
                        new_del_ti2.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\判断题题库.docx')                        
                    else:
                        dangerous_box(7)
                        break
                   
                    new_count=0   
                    list_del=[]
                    new_list_del=[]
                    new_list_del2=[]
                    tihao=1
        if(type_==4):    
            if(check_jd_tiku()=='jd_N'):
                print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
                print('按任意键继续...')
                os.system('pause')
                return
            else:     
                while(cancel):
                    del_ti=Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\简答题题库.docx')
                    for jianda in del_ti.paragraphs:  #获取总共有多少题count
                        if panduan_kong(jianda.text)==True :
                            new_count=new_count+1
                        else:
                            new_count = new_count
                    delete_num=input('一共有'+str(new_count)+'题，请输入需要删除的题号，注意一次只能删除一题，退出请直接按回车：\n')
                    if(len(str(delete_num))!=0):
                        for ti_ in del_ti.paragraphs:
                            list_del.append(ti_.text)
                        list_del.pop(int(delete_num)-1)
                        for i in list_del:
                            if len(i)!=0:
                                new_list_del.append(i)
                        new_del_ti=Document()
                        for new_ti_ in new_list_del:
                            new=new_del_ti.add_paragraph(new_ti_)
                        print('删除成功')
                        new_del_ti.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\简答题题库.docx')
                        for x in new_del_ti.paragraphs:
                            if x.text[0].isdigit()==True and x.text[1].isdigit()==False and x.text[2].isdigit()==False:
                                new_ti_hao=x.text.replace(x.text[0],str(tihao),1)
                            if x.text[0].isdigit()==True and x.text[1].isdigit()==True and x.text[2].isdigit()==False:
                                new_ti_hao=x.text.replace(x.text[0]+x.text[1],str(tihao),1)
                            if x.text[0].isdigit()==True and x.text[1].isdigit()==True and x.text[2].isdigit()==True:
                                new_ti_hao=x.text.replace(x.text[0]+x.text[1]+x.text[2],str(tihao),1)
                            new_list_del2.append(new_ti_hao)
                            tihao=tihao+1
                        new_del_ti2=Document()
                        for new_ti_ in new_list_del2:
                            new=new_del_ti2.add_paragraph(new_ti_)
                        new_del_ti2.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\简答题题库.docx')                        
                    else:
                        dangerous_box(7)
                        break
                        
                    new_count=0   
                    list_del=[]
                    new_list_del=[]
                    new_list_del2=[]
                    tihao=1

###################################################################################################
    while(True):
        set_tiku=input('1、题库管理     2、创建题库     3、题库重复题自检     4、返回主菜单\n')
        if(set_tiku.isdigit()==False or int(set_tiku)>4 or int(set_tiku)<=0):
            print('输入错误，请重新输入，注意输入数字！')
        else:
            set_tiku=int(set_tiku)
            break
    if(set_tiku==1):
        while(True):
            mana_tiku_choice=input('1、管理【填空题】题库     2、管理【选择题】题库     3、管理【判断题】题库     4、管理【简答题】题库     5、返回主菜单\n')
            if(mana_tiku_choice.isdigit()==False or int(mana_tiku_choice)>5 or int(mana_tiku_choice)<=0):
                print('输入错误，请重新输入，注意输入数字！')
            else:
                mana_tiku_choice=int(mana_tiku_choice)
                break
        if(mana_tiku_choice==1):
            while(True):
                mana_tiankong_tiku_choice=input('1、显示所有题     2、增加题     3、删除指定题     4、返回主菜单\n')
                if(mana_tiankong_tiku_choice.isdigit()==False or int(mana_tiankong_tiku_choice)>4 or int(mana_tiankong_tiku_choice)<=0):
                    print('输入错误，请重新输入，注意输入数字！')
                else:
                    mana_tiankong_tiku_choice=int(mana_tiankong_tiku_choice)
                if(mana_tiankong_tiku_choice==1):
                    print('\n')
                    display(1)
                    print('\n')
                if(mana_tiankong_tiku_choice==2):
                    add_new_ti(1)  #单独的添加函数
                    return  
                elif(mana_tiankong_tiku_choice==3):
                    delete_q(1)     #单独的删除函数
                    return
                elif(mana_tiankong_tiku_choice==4):
                    print('将返回主菜单')
                    delay_i=3
                    for x in range(3):
                        print('\r'+str(delay_i)+'秒钟后将返回主菜单...',end='')#不换行输入显示倒计时，\r表示回车，即每次输入换行，但是末尾end=''将换行取消
                        delay_i=delay_i-1                                                       #所以两个组合表示将输入光标移动到行首，并且不换行
                        time.sleep(1)
                    return
        elif(mana_tiku_choice==2):
            while(True):
                mana_xuanze_tiku_choice=input('1、显示所有题     2、增加题     3、删除指定题     4、返回主菜单\n')
                if(mana_xuanze_tiku_choice.isdigit()==False or int(mana_xuanze_tiku_choice)>4 or int(mana_xuanze_tiku_choice)<=0):
                    print('输入错误，请重新输入，注意输入数字！')
                else:
                    mana_xuanze_tiku_choice=int(mana_xuanze_tiku_choice)
                if(mana_xuanze_tiku_choice==1):
                    print('\n')
                    display(2)
                    print('\n')
                if(mana_xuanze_tiku_choice==2):
                    add_new_ti(2)  #单独的添加函数
                    return  
                elif(mana_xuanze_tiku_choice==3):
                    delete_q(2)    #单独的删除函数
                    return
                elif(mana_xuanze_tiku_choice==4):
                    print('将返回主菜单')
                    delay_i=3
                    for x in range(3):
                        print('\r'+str(delay_i)+'秒钟后将返回主菜单...',end='')#不换行输入显示倒计时，\r表示回车，即每次输入换行，但是末尾end=''将换行取消
                        delay_i=delay_i-1                                                       #所以两个组合表示将输入光标移动到行首，并且不换行
                        time.sleep(1)
                    return
        elif(mana_tiku_choice==3):
            while(True):
                mana_panduan_tiku_choice=input('1、显示所有题     2、增加题     3、删除指定题     4、返回主菜单\n')
                if(mana_panduan_tiku_choice.isdigit()==False or int(mana_panduan_tiku_choice)>4 or int(mana_panduan_tiku_choice)<=0):
                    print('输入错误，请重新输入，注意输入数字！')
                else:
                    mana_panduan_tiku_choice=int(mana_panduan_tiku_choice)
                if(mana_panduan_tiku_choice==1):
                    print('\n')
                    display(3)
                    print('\n')
                if(mana_panduan_tiku_choice==2):
                    add_new_ti(3)  #单独的添加函数
                    return  
                elif(mana_panduan_tiku_choice==3):
                    delete_q(3)     #单独的删除函数
                    return
                elif(mana_panduan_tiku_choice==4):
                    print('将返回主菜单')
                    delay_i=3
                    for x in range(3):
                        print('\r'+str(delay_i)+'秒钟后将返回主菜单...',end='')#不换行输入显示倒计时，\r表示回车，即每次输入换行，但是末尾end=''将换行取消
                        delay_i=delay_i-1                                                       #所以两个组合表示将输入光标移动到行首，并且不换行
                        time.sleep(1)
                    return
        elif(mana_tiku_choice==4):
            while(True):
                mana_jianda_tiku_choice=input('1、显示所有题     2、增加题     3、删除指定题     4、返回主菜单\n')
                if(mana_jianda_tiku_choice.isdigit()==False or int(mana_jianda_tiku_choice)>4 or int(mana_jianda_tiku_choice)<=0):
                    print('输入错误，请重新输入，注意输入数字！')
                else:
                    mana_jianda_tiku_choice=int(mana_jianda_tiku_choice)
                if(mana_jianda_tiku_choice==1):
                    print('\n')
                    display(4)
                    print('\n')
                if(mana_jianda_tiku_choice==2):
                    add_new_ti(4)  #单独的添加函数
                    return  
                elif(mana_jianda_tiku_choice==3):
                    delete_q(4)     #单独的删除函数
                    return
                elif(mana_jianda_tiku_choice==4):
                    print('将返回主菜单')
                    delay_i=3
                    for x in range(3):
                        print('\r'+str(delay_i)+'秒钟后将返回主菜单...',end='')#不换行输入显示倒计时，\r表示回车，即每次输入换行，但是末尾end=''将换行取消
                        delay_i=delay_i-1                                                       #所以两个组合表示将输入光标移动到行首，并且不换行
                        time.sleep(1)
                    return
        elif(mana_tiku_choice==5):
                print('将返回主菜单')
                delay_i=3
                for x in range(3):
                    print('\r'+str(delay_i)+'秒钟后将返回主菜单...',end='')#不换行输入显示倒计时，\r表示回车，即每次输入换行，但是末尾end=''将换行取消
                    delay_i=delay_i-1                                                       #所以两个组合表示将输入光标移动到行首，并且不换行
                    time.sleep(1)
                return
    elif(set_tiku==2):
        if(os.path.exists(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\填空题题库.docx')==True
            or  os.path.dirname(os.path.realpath(sys.argv[0]))+'\\选择题题库.docx'==True
            or  os.path.dirname(os.path.realpath(sys.argv[0]))+'\\简答题题库.docx'==True
            or  os.path.dirname(os.path.realpath(sys.argv[0]))+'\\判断题题库.docx'==True):
            print('当前软件子文件夹中可能已存在【填空题】、【选择题】、【判断题】、【简答题】题库文件，如需创建将覆盖旧题库，请确认:')
        while(True):
            set_tiku_YN=input('\nY、确认创建     N、取消创建（返回主菜单）\n')
            if(set_tiku_YN =='Y' or set_tiku_YN=='y' or set_tiku_YN=='N' or set_tiku_YN=='n'):
                break
            else:
                print('输入错误，请重新输入，注意输入Y或N！')
        print(set_tiku_YN)
        if (set_tiku_YN=='Y' or set_tiku_YN=='y'):
            if(os.path.exists(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\填空题题库.docx')==True
            or  os.path.dirname(os.path.realpath(sys.argv[0]))+'\\选择题题库.docx'==True
            or  os.path.dirname(os.path.realpath(sys.argv[0]))+'\\简答题题库.docx'==True
            or  os.path.dirname(os.path.realpath(sys.argv[0]))+'\\判断题题库.docx'==True):
                while(True):
                    print('当前子文件夹中已存在四种题库的一项或多项，因此再需创建需输入密码或将子文件夹中的题库剪切至其他地方或删除\n')
                    code=getpass.getpass('请输入密码(密码不会显示，输入完请按回车,返回主菜单请直接按回车键)：')
                    print('*'*len(code))
                    if(code=='rainbow6'):
                        print('密码正确，请继续...')
                        break
                    elif(len(code)==0):
                        print('取消创建，将返回主菜单')
                        delay_i=3
                        for x in range(3):
                            print('\r'+str(delay_i)+'秒钟后将返回主菜单...',end='')#不换行输入显示倒计时，\r表示回车，即每次输入换行，但是末尾end=''将换行取消
                            delay_i=delay_i-1                                                       #所以两个组合表示将输入光标移动到行首，并且不换行
                            time.sleep(1)
                        return
                    else:
                        print('密码错误，请重新输入,退出直接关闭程序')
            while(True):
                set_tiku_choice=input('1、创建【填空题】题库     2、创建【选择题】题库     3、创建【判断题】题库     4、创建【简答题】题库     5、取消创建\n')
                if(set_tiku_choice.isdigit()==False or int(set_tiku_choice)>5 or int(set_tiku_choice)<=0):
                    print('输入错误，请重新输入，注意输入数字！')
                else:
                    set_tiku_choice=int(set_tiku_choice)
                    break
            if(set_tiku_choice==1):
                setup_tiankong_tiku = Document()
                setup_tiankong_tiku.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\填空题题库.docx')
                print('创建【填空题】题库成功，请添加题目，注意格式，不需要写题号，答案处注意加括号')
                dangerous_box(6)
                add_q(1)
            if(set_tiku_choice==2):
                setup_xuanze_tiku = Document()
                setup_xuanze_tiku.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\选择题题库.docx')
                print('创建【选择题】题库成功，请添加题目，注意格式，不需要写题号，答案处注意加括号')
                dangerous_box(6)
                add_q(2)
            if(set_tiku_choice==3):
                setup_panduan_tiku = Document()
                setup_panduan_tiku.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\判断题题库.docx')
                print('创建【判断题】题库成功，请添加题目，注意格式，不需要写题号，答案处注意加括号')
                dangerous_box(6)
                add_q(3)
            if(set_tiku_choice==4):
                setup_jianda_tiku = Document()
                setup_jianda_tiku.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\简答题题库.docx')
                print('创建【简答题】题库成功，请添加题目，注意格式，不需要写题号，答案处注意加括号')
                dangerous_box(6)
                add_q(4)
            if(set_tiku_choice==5):
                print('取消创建，将返回主菜单')
                delay_i=3
                for x in range(3):
                    print('\r'+str(delay_i)+'秒钟后将返回主菜单...',end='')#不换行输入显示倒计时，\r表示回车，即每次输入换行，但是末尾end=''将换行取消
                    delay_i=delay_i-1                                                       #所以两个组合表示将输入光标移动到行首，并且不换行
                    time.sleep(1)
                return    
        elif set_tiku_YN=='N' or set_tiku_YN=='n':
            print('已取消创建题库，将返回主菜单\n')
            delay_i=3
            for x in range(3):
                print('\r'+str(delay_i)+'秒钟后返回主菜单...',end='')#不换行输入显示倒计时，\r表示回车，即每次输入换行，但是末尾end=''将换行取消
                delay_i=delay_i-1                                                       #所以两个组合表示将输入光标移动到行首，并且不换行
                time.sleep(1)
            return
    elif(set_tiku==3):
        remove_chongfu()
    elif(set_tiku==4):
        print('\n已返回主菜单...\n')

###############初始化题库###########################################
def init_tiku():#解决错误题号问题
    if(check_tk_tiku()=='tk_N'):
        print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
        os.system('pause')
    else:
        ti=''
        list_init_1=[]
        list_init_2=[]
        count_init=1
        count_init_cw=0
        list_init_cw=[]
        new_ti_init=''
        try:
            tiankong_init = Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\填空题题库.docx')
            for ti in tiankong_init.paragraphs:
                if len(ti.text)!=0 :
                    list_init_1.append(ti.text)
            for x in list_init_1:
                    if x[0].isdigit()==False :
                                new_ti_init=str(count_init)+'、'+x
                    elif x[0].isdigit()==True  and x[1].isdigit()==True and x[2].isdigit()==True  and x[3]!='、' and x[3]!='.' :
                                new_ti_init=str(count_init)+'、'+x      
                    elif x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==False and x[2]!='、' and x[2]!='.' :
                                new_ti_init=str(count_init)+'、'+x 
                    elif x[0].isdigit()==True and x[1].isdigit()==False and x[1]!='、' and x[1]!='.' :
                                new_ti_init=str(count_init)+'、'+x                                    
                    elif x[0].isdigit()==True and x[1].isdigit()==False :
                                new_ti_init=x.replace(x[0],str(count_init),1)
                    elif x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==False:
                                new_ti_init=x.replace(x[0]+x[1],str(count_init),1)
                    elif x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==True:
                                new_ti_init=x.replace(x[0]+x[1]+x[2],str(count_init),1)
                    list_init_2.append(new_ti_init)
                    count_init=count_init+1
            list_init_3=[]
            for x in list_init_2 :
                quanjiao_l=''#题库内半角括号统一转换为全角
                quanjiao_r=''
                quanjiao_l=x.replace('(','（')
                quanjiao_r=quanjiao_l.replace(')','）')
                if quanjiao_r[1]=='.':
                    list_init_3.append(quanjiao_r.replace('.','、',1))
                elif quanjiao_r[2]=='.':
                    list_init_3.append(quanjiao_r.replace('.','、',1))
                elif quanjiao_r[3]=='.':
                    list_init_3.append(quanjiao_r.replace('.','、',1))
                else:
                    list_init_3.append(quanjiao_r)
            count_init=0
            init_tiankong=Document()
            for init_tk in list_init_3:
                new=init_tiankong.add_paragraph(init_tk)
            init_tiankong.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\填空题题库.docx')   
            tiankongti = Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\填空题题库.docx')
            for tiankong in tiankongti.paragraphs:#获取总共有多少题count
                if panduan_kong(tiankong.text)==True or len(tiankong.text.strip())!=0:
                    count_init_cw=count_init_cw+1
                    if tiankong.text.find('(')== -1 and tiankong.text.find('（')== -1 and tiankong.text.find(')')== -1 and tiankong.text.find('）')== -1:
                        list_init_cw.append(count_init_cw)
                    elif (tiankong.text.count('(')== 0 and tiankong.text.count('（')== 0) or (tiankong.text.count(')')== 0 and tiankong.text.count('）')== 0):
                        list_init_cw.append(count_init_cw)
                else:
                    count_init_cw = count_init_cw
            if(len(list_init_cw)!=0):
                        dangerous_box(1)
                        print('\n【填空题】题库中以下题目存在格式错误：【题干内缺少括号】 \n')
                        print(list_init_cw)
                        print('\n题库初始化失败，请将【填空题】题库错误格式的题目更改后再重新初始化题库\n')
                        R_tiku(1)  
            print('【填空题】题库初始化成功')
        except(SystemExit):
            sys.exit(0)



    if(check_xz_tiku()=='xz_N'):
        print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
        os.system('pause')
    else:
        ti=''
        list_init_1=[]
        list_init_2=[]
        count_init=1
        count_init_cw=0
        list_init_cw_dian=[]
        list_init_cw_kuohao=[]
        list_init_cw_ABCD=[]
        new_ti_init=''
        try:
            xuanze_init = Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\选择题题库.docx')
            for ti in xuanze_init.paragraphs:
                if len(ti.text)!=0 :
                    list_init_1.append(ti.text)
            for x in list_init_1:
                    if x[0].isdigit()==False :
                                new_ti_init=str(count_init)+'、'+x
                    elif x[0].isdigit()==True  and x[1].isdigit()==True and x[2].isdigit()==True  and x[3]!='、' and x[3]!='.' :
                                new_ti_init=str(count_init)+'、'+x      
                    elif x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==False and x[2]!='、' and x[2]!='.' :
                                new_ti_init=str(count_init)+'、'+x 
                    elif x[0].isdigit()==True and x[1].isdigit()==False and x[1]!='、' and x[1]!='.' :
                                new_ti_init=str(count_init)+'、'+x                                    
                    elif x[0].isdigit()==True and x[1].isdigit()==False :
                                new_ti_init=x.replace(x[0],str(count_init),1)
                    elif x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==False:
                                new_ti_init=x.replace(x[0]+x[1],str(count_init),1)
                    elif x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==True:
                                new_ti_init=x.replace(x[0]+x[1]+x[2],str(count_init),1)
                    list_init_2.append(new_ti_init)
                    count_init=count_init+1
            list_init_3=[]
            for x in list_init_2 :
                quanjiao_l=''
                quanjiao_r=''
                quanjiao_l=x.replace('(','（')
                quanjiao_r=quanjiao_l.replace(')','）')
                if quanjiao_r[1]=='.':
                    list_init_3.append(quanjiao_r.replace('.','、',1))
                elif quanjiao_r[2]=='.':
                    list_init_3.append(quanjiao_r.replace('.','、',1))
                elif quanjiao_r[3]=='.':
                    list_init_3.append(quanjiao_r.replace('.','、',1))
                else:
                    list_init_3.append(quanjiao_r)
            count_init=0
            init_xuanze=Document()
            for init_xz in list_init_3:
                new=init_xuanze.add_paragraph(init_xz)
            init_xuanze.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\选择题题库.docx')      
            xuanzeti = Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\选择题题库.docx')
            for xuanze in xuanzeti.paragraphs:#获取总共有多少题cunt
                if panduan_kong(xuanze.text)==True or len(xuanze.text.strip())!=0:
                    count_init_cw=count_init_cw+1
                    if xuanze.text.find('&')== -1 or xuanze.text.count('&')>1:
                        list_init_cw_dian.append(count_init_cw)                  
                    elif xuanze.text.find('(')== -1 and xuanze.text.find('（')== -1 and xuanze.text.find(')')== -1 and xuanze.text.find('）')== -1:
                        list_init_cw_kuohao.append(count_init_cw)
                    elif (xuanze.text.count('(')== 0 and xuanze.text.count('（')== 0) or (xuanze.text.count(')')== 0 and xuanze.text.count('）')== 0):
                        list_init_cw_kuohao.append(count_init_cw)
                    else:
                        xuanze_xuanxiang=xuanze.text.split('&')
                        if xuanze_xuanxiang[1].count('A')>1 or xuanze_xuanxiang[1].count('B')>1 or xuanze_xuanxiang[1].count('C')>1 or xuanze_xuanxiang[1].count('D')>1:
                            list_init_cw_ABCD.append(count_init_cw)
                else:
                    count_init_cw = count_init_cw
            if (len(list_init_cw_dian)!=0):
                        dangerous_box(2)
                        print('\n【选择题】题库中以下题目存在格式错误：【题干与选项之间缺少符号“&”隔开 】\n或【&符号位置或数量不正确，每题中仅可有一个&】 \n')
                        print(list_init_cw_dian)
                        print('\n题库初始化失败，请将【选择题】题库错误格式的题目更改后再重新初始化题库\n')
                        print('{:*^50}'.format('创建失败')) 
                        R_tiku(1)
            if (len(list_init_cw_kuohao)!=0):
                        dangerous_box(2)
                        print('\n【选择题】题库中以下题目存在格式错误：【题干内缺少括号】\n')
                        print(list_init_cw_kuohao)
                        print('\n题库初始化失败，请将【选择题】题库内有错误格式的题目更改后再重新初始化题库\n')    
                        R_tiku(1)       
            elif (len(list_init_cw_ABCD)!=0):
                        dangerous_box(2)
                        print('\n【选择题】题库中以下题目存在格式错误：【选项内容中出现多个大写ABCD】,可用小写字母代替\n')
                        print(list_init_cw_ABCD)
                        print('\n题库初始化失败，请将【选择题】题库内有错误格式的题目更改后再重新初始化题库\n')    
                        R_tiku(1)                  
            print('【选择题】题库初始化成功')
        except(SystemExit):
            sys.exit(0)


    if(check_pd_tiku()=='pd_N'):
        print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
        os.system('pause')
    else:
        ti=''
        list_init_1=[]
        list_init_2=[]
        count_init=1
        count_init_cw=0
        list_init_cw=[]
        new_ti_init=''
        try:
            panduan_init = Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\判断题题库.docx')
            for ti in panduan_init.paragraphs:
                if len(ti.text)!=0 :
                    list_init_1.append(ti.text)

            for x in list_init_1:
                    if x[0].isdigit()==False :
                                new_ti_init=str(count_init)+'、'+x
                    elif x[0].isdigit()==True  and x[1].isdigit()==True and x[2].isdigit()==True  and x[3]!='、' and x[3]!='.' :
                                new_ti_init=str(count_init)+'、'+x      
                    elif x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==False and x[2]!='、' and x[2]!='.' :
                                new_ti_init=str(count_init)+'、'+x 
                    elif x[0].isdigit()==True and x[1].isdigit()==False and x[1]!='、' and x[1]!='.' :
                                new_ti_init=str(count_init)+'、'+x                                    
                    elif x[0].isdigit()==True and x[1].isdigit()==False :
                                new_ti_init=x.replace(x[0],str(count_init),1)
                    elif x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==False:
                                new_ti_init=x.replace(x[0]+x[1],str(count_init),1)
                    elif x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==True:
                                new_ti_init=x.replace(x[0]+x[1]+x[2],str(count_init),1)
                    list_init_2.append(new_ti_init)
                    count_init=count_init+1
            #print(list_init_2)
            list_init_3=[]
            for x in list_init_2 :
                quanjiao_l=''
                quanjiao_r=''
                quanjiao_l=x.replace('(','（')
                quanjiao_r=quanjiao_l.replace(')','）')
                if quanjiao_r[1]=='.':
                    list_init_3.append(quanjiao_r.replace('.','、',1))
                elif quanjiao_r[2]=='.':
                    list_init_3.append(quanjiao_r.replace('.','、',1))
                elif quanjiao_r[3]=='.':
                    list_init_3.append(quanjiao_r.replace('.','、',1))
                else:
                    list_init_3.append(quanjiao_r)
            count_init=0
            init_panduan=Document()
            for init_pd in list_init_3:
                new=init_panduan.add_paragraph(init_pd)
            init_panduan.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\判断题题库.docx')     
            panduanti = Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\判断题题库.docx')
            for panduan in panduanti.paragraphs:#获取总共有多少题count
                if panduan_kong(panduan.text)==True or len(panduan.text.strip())!=0:
                    count_init_cw=count_init_cw+1
                    if panduan.text.find('(')== -1 and panduan.text.find('（')== -1 and panduan.text.find(')')== -1 and panduan.text.find('）')== -1:
                        list_init_cw.append(count_init_cw)
                    elif (panduan.text.count('(')== 0 and panduan.text.count('（')== 0) or (panduan.text.count(')')== 0 and panduan.text.count('）')== 0):
                        list_init_cw.append(count_init_cw)
                else:
                    count_init_cw = count_init_cw
            if(len(list_init_cw)!=0):
                        dangerous_box(9)
                        print('\n【判断题】题库中以下题目存在格式错误：【题干内缺少括号】 \n')
                        print(list_init_cw)
                        print('\n题库初始化失败，请将【判断题】题库错误格式的题目更改后再重新初始化题库\n')
                        R_tiku(1)      
            print('【判断题】题库初始化成功')
        except(SystemExit):
            sys.exit(0)
 
    if(check_jd_tiku()=='jd_N'):
        print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
        print('将返回主菜单...')
    else:
        ti=''
        list_init_1=[]
        list_init_2=[]
        count_init=1
        count_init_cw=0
        list_init_cw=[]
        new_ti_init=''
        try:
            jianda_init = Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\简答题题库.docx')
            for ti in jianda_init.paragraphs:
                if len(ti.text)!=0 :
                    list_init_1.append(ti.text)
            for x in list_init_1:
                    if x[0].isdigit()==False :
                                new_ti_init=str(count_init)+'、'+x
                    elif x[0].isdigit()==True  and x[1].isdigit()==True and x[2].isdigit()==True  and x[3]!='、' and x[3]!='.' :
                                new_ti_init=str(count_init)+'、'+x      
                    elif x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==False and x[2]!='、' and x[2]!='.' :
                                new_ti_init=str(count_init)+'、'+x 
                    elif x[0].isdigit()==True and x[1].isdigit()==False and x[1]!='、' and x[1]!='.' :
                                new_ti_init=str(count_init)+'、'+x                                    
                    elif x[0].isdigit()==True and x[1].isdigit()==False :
                                new_ti_init=x.replace(x[0],str(count_init),1)
                    elif x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==False:
                                new_ti_init=x.replace(x[0]+x[1],str(count_init),1)
                    elif x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==True:
                                new_ti_init=x.replace(x[0]+x[1]+x[2],str(count_init),1)
                    list_init_2.append(new_ti_init)
                    count_init=count_init+1
            list_init_3=[]
            for x in list_init_2 :
                quanjiao_l=''
                quanjiao_r=''
                quanjiao_l=x.replace('(','（')
                quanjiao_r=quanjiao_l.replace(')','）')
                if quanjiao_r[1]=='.':
                    list_init_3.append(quanjiao_r.replace('.','、',1))
                elif quanjiao_r[2]=='.':
                    list_init_3.append(quanjiao_r.replace('.','、',1))
                elif quanjiao_r[3]=='.':
                    list_init_3.append(quanjiao_r.replace('.','、',1))
                else:
                    list_init_3.append(quanjiao_r)
            count_init=0
            init_jianda=Document()
            for init_jd in list_init_3:
                new=init_jianda.add_paragraph(init_jd)
            init_jianda.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\简答题题库.docx')  
            jiandati = Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\简答题题库.docx')
            for jianda in jiandati.paragraphs:#获取总共有多少题cunt
                if panduan_kong(jianda.text)==True or len(jianda.text.strip())!=0:
                    count_init_cw=count_init_cw+1
                    if jianda.text.find('&')== -1 or jianda.text.count('&')>1:
                        list_init_cw.append(count_init_cw)
                else:
                    count_init_cw = count_init_cw
            if(len(list_init_cw)!=0):
                        dangerous_box(3)
                        print('\n【简答题】题库中以下题目存在格式错误：【题干与答案之间缺少符号“&” 隔开】\n或【&符号位置或数量不正确，每题中仅可有一个&】\n')
                        print(list_init_cw)
                        print('\n题库初始化失败，请将【简答题】题库错误格式的题目更改后再重新初始化题库\n')   
                        R_tiku(1)   
            print('【简答题】题库初始化成功')
        except(SystemExit):
            sys.exit(0)

    return
def R_tiku(i):
    if(i==1):
        print('请检查对应题库格式')
    elif(i==2):
        print('请检查啊题库文件是否正常')
    os.system('pause')
    delay_i=3
    for x in range(3):
        print('\r谢谢使用，'+str(delay_i)+'秒钟后将自动退出程序...',end='')#不换行输入显示倒计时，\r表示回车，即每次输入换行，但是末尾end=''将换行取消
        delay_i=delay_i-1                                                       #所以两个组合表示将输入光标移动到行首，并且不换行
        time.sleep(1)
    sys.exit(0)
#################################################################
def remove_chongfu():
    if(check_tk_tiku()=='tk_N'):
        print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
        print('按任意键继续...')
        os.system('pause')
    else:
        ti=''
        list_chongfu_1=[]
        list_chongfu_2=[]
        list_chongfu_3=[]
        list_chongfu_3_1=[]
        list_chongfu_3_2=[]
        list_chongfu_4=[]
        count_chongfu=1
        tiankong_init = Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\填空题题库.docx')
        for ti in tiankong_init.paragraphs:
            if len(ti.text)!=0 :
                list_chongfu_1.append(ti.text)
        for x in list_chongfu_1:
                if x[0].isdigit()==True and x[1].isdigit()==False :
                            new_ti_init=x.replace(x[0],str('n'),1)
                if x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==False:
                            new_ti_init=x.replace(x[0]+x[1],str('n'),1)
                if x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==True:
                            new_ti_init=x.replace(x[0]+x[1]+x[2],str('n'),1)
                list_chongfu_2.append(''.join(new_ti_init.split()))
                list_chongfu_3=set(list_chongfu_2)

        for ii in list_chongfu_3:
            if ii[:8].replace(' ','') not in list_chongfu_3_1:
                list_chongfu_3_2.append(ii)
        count_cf = 0
        list_cf_1=list_chongfu_3_2
        list_cf_2=[]
        list_cf_2_1=[]
        list_cf_3=[]
        list_cf_1=set(list_cf_1)#粗略去重
        for i in list_cf_1:
            list_cf_2.append(i[:8])
        list_cf_2=set(list_cf_2)
        for x in list_cf_2:
            list_cf_2_1.append(x)
        for ii in list_cf_1:
            count_cf=0
            for iii in list_cf_2_1:
                if ii[:8]==iii:
                    list_cf_3.append(ii)
                    list_cf_2_1.pop(count_cf)
                count_cf=count_cf+1                                   
        for y in list_cf_3:
                if y[0]=='n' and y[1]!='n' and y[2]!='n':
                            new_ti_init=y.replace(y[0],str(count_chongfu))
                if y[0]=='n' and y[1]=='n' and y[2]!='n':
                            new_ti_init=y.replace(x[0]+y[1],str(count_chongfu))
                if y[0]=='n' and y[1]=='n' and y[2]=='n':
                            new_ti_init=y.replace(y[0]+y[1]+y[2],str(count_chongfu))
                list_chongfu_4.append(new_ti_init)
                count_chongfu=count_chongfu+1
        count_chongfu=0
        chongfu_tiankong=Document()
        for chongfu_tk in list_chongfu_4:
            new=chongfu_tiankong.add_paragraph(chongfu_tk)
        chongfu_tiankong.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\填空题题库.docx')   
        print('【填空题】题库重复题已经概略清除，如果要严格清除重复题，请手动打开题库文件并清除')
    
    if(check_xz_tiku()=='xz_N'):
        print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
        print('按任意键继续...')
        os.system('pause')
    else:
        ti=''
        list_chongfu_1=[]
        list_chongfu_2=[]
        list_chongfu_3=[]
        list_chongfu_3_1=[]
        list_chongfu_3_2=[]
        list_chongfu_4=[]
        count_chongfu=1
        xuanze_init = Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\选择题题库.docx')
        for ti in xuanze_init.paragraphs:
            if len(ti.text)!=0 :
                list_chongfu_1.append(ti.text)
        for x in list_chongfu_1:
                if x[0].isdigit()==True and x[1].isdigit()==False :
                            new_ti_init=x.replace(x[0],str('n'),1)
                if x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==False:
                            new_ti_init=x.replace(x[0]+x[1],str('n'),1)
                if x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==True:
                            new_ti_init=x.replace(x[0]+x[1]+x[2],str('n'),1)
        
                list_chongfu_2.append(''.join(new_ti_init.split()))
                list_chongfu_3=set(list_chongfu_2)
        for ii in list_chongfu_3:
            if ii[:8].replace(' ','') not in list_chongfu_3:
                list_chongfu_3_2.append(ii)

        count_cf = 0
        list_cf_1=list_chongfu_3_2
        list_cf_2=[]
        list_cf_2_1=[]
        list_cf_3=[]
        list_cf_1=set(list_cf_1)#粗略去重
        for i in list_cf_1:
            list_cf_2.append(i[:8])
        list_cf_2=set(list_cf_2)
        for x in list_cf_2:
            list_cf_2_1.append(x)
        for ii in list_cf_1:
            count_cf=0
            for iii in list_cf_2_1:
                if ii[:8]==iii:
                    list_cf_3.append(ii)
                    list_cf_2_1.pop(count_cf)
                count_cf=count_cf+1                                   
        for y in list_cf_3:
                if y[0]=='n' and y[1]!='n' and y[2]!='n':
                            new_ti_init=y.replace(y[0],str(count_chongfu))
                if y[0]=='n' and y[1]=='n' and y[2]!='n':
                            new_ti_init=y.replace(x[0]+y[1],str(count_chongfu))
                if y[0]=='n' and y[1]=='n' and y[2]=='n':
                            new_ti_init=y.replace(y[0]+y[1]+y[2],str(count_chongfu))
                list_chongfu_4.append(new_ti_init)
                count_chongfu=count_chongfu+1
        #print(list_chongfu_4)
        count_chongfu=0
        chongfu_xuanze=Document()
        for chongfu_xz in list_chongfu_4:
            if chongfu_xz.find('D')==-1:
                new=chongfu_xuanze.add_paragraph(chongfu_xz.split('&')[0]+'&'+chongfu_xz.split('&')[1][:chongfu_xz.split('&')[1].find('B')]+'   '+chongfu_xz.split('&')[1][chongfu_xz.split('&')[1].find('B'):chongfu_xz.split('&')[1].find('C')]+'   '+chongfu_xz.split('&')[1][chongfu_xz.split('&')[1].find('C'):])
            else :
                new=chongfu_xuanze.add_paragraph(chongfu_xz.split('&')[0]+'&'+chongfu_xz.split('&')[1][:chongfu_xz.split('&')[1].find('B')]+'   '+chongfu_xz.split('&')[1][chongfu_xz.split('&')[1].find('B'):chongfu_xz.split('&')[1].find('C')]+'   '+chongfu_xz.split('&')[1][chongfu_xz.split('&')[1].find('C'):chongfu_xz.split('&')[1].find('D')]+'   '+chongfu_xz.split('&')[1][chongfu_xz.split('&')[1].find('D'):])
        chongfu_xuanze.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\选择题题库.docx')   
        print('【选择题】题库重复题已经概略清除，如果要严格清除重复题，请手动打开题库文件并清除')
    if(check_pd_tiku()=='pd_N'):
        print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
        print('按任意键继续...')
        os.system('pause')
    else:
        ti=''
        list_chongfu_1=[]
        list_chongfu_2=[]
        list_chongfu_3=[]
        list_chongfu_3_1=[]
        list_chongfu_3_2=[]
        list_chongfu_4=[]
        count_chongfu=1
        panduan_init = Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\判断题题库.docx')
        for ti in panduan_init.paragraphs:
            if len(ti.text)!=0 :
                list_chongfu_1.append(ti.text)
        for x in list_chongfu_1:
                if x[0].isdigit()==True and x[1].isdigit()==False :
                            new_ti_init=x.replace(x[0],str('n'),1)
                if x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==False:
                            new_ti_init=x.replace(x[0]+x[1],str('n'),1)
                if x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==True:
                            new_ti_init=x.replace(x[0]+x[1]+x[2],str('n'),1)
                list_chongfu_2.append(''.join(new_ti_init.split()))
                list_chongfu_3=set(list_chongfu_2)
        for ii in list_chongfu_3:
            if ii[:8].replace(' ','') not in list_chongfu_3_1:
                list_chongfu_3_2.append(ii)
        count_cf = 0
        list_cf_1=list_chongfu_3_2
        list_cf_2=[]
        list_cf_2_1=[]
        list_cf_3=[]
        list_cf_1=set(list_cf_1)#粗略去重
        for i in list_cf_1:
            list_cf_2.append(i[:8])
        list_cf_2=set(list_cf_2)
        for x in list_cf_2:
            list_cf_2_1.append(x)
        for ii in list_cf_1:
            count_cf=0
            for iii in list_cf_2_1:
                if ii[:8]==iii:
                    list_cf_3.append(ii)
                    list_cf_2_1.pop(count_cf)
                count_cf=count_cf+1                                   
        for y in list_cf_3:
                if y[0]=='n' and y[1]!='n' and y[2]!='n':
                            new_ti_init=y.replace(y[0],str(count_chongfu))
                if y[0]=='n' and y[1]=='n' and y[2]!='n':
                            new_ti_init=y.replace(x[0]+y[1],str(count_chongfu))
                if y[0]=='n' and y[1]=='n' and y[2]=='n':
                            new_ti_init=y.replace(y[0]+y[1]+y[2],str(count_chongfu))
                list_chongfu_4.append(new_ti_init)
                count_chongfu=count_chongfu+1
        count_chongfu=0
        chongfu_panduan=Document()
        for chongfu_pd in list_chongfu_4:
            new=chongfu_panduan.add_paragraph(chongfu_pd)
        chongfu_panduan.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\判断题题库.docx')   
        print('【判断题】题库重复题已经概略清除，如果要严格清除重复题，请手动打开题库文件并清除')
    if(check_jd_tiku()=='jd_N'):
        print('请按规定格式创建题库或将题库和本程序放置于同一子文件夹下')
        print('将返回主菜单...')
    else:
        ti=''
        list_chongfu_1=[]
        list_chongfu_2=[]
        list_chongfu_3=[]
        list_chongfu_3_1=[]
        list_chongfu_3_2=[]
        list_chongfu_4=[]
        count_chongfu=1
        jianda_init = Document(os.path.dirname(os.path.realpath(sys.argv[0]))+'\\简答题题库.docx')
        for ti in jianda_init.paragraphs:
            if len(ti.text)!=0 :
                list_chongfu_1.append(ti.text)
        for x in list_chongfu_1:
                if x[0].isdigit()==True and x[1].isdigit()==False :
                            new_ti_init=x.replace(x[0],str('n'),1)
                if x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==False:
                            new_ti_init=x.replace(x[0]+x[1],str('n'),1)
                if x[0].isdigit()==True and x[1].isdigit()==True and x[2].isdigit()==True:
                            new_ti_init=x.replace(x[0]+x[1]+x[2],str('n'),1)
                list_chongfu_2.append(''.join(new_ti_init.split()))
                list_chongfu_3=set(list_chongfu_2)
        for ii in list_chongfu_3:
            if ii[:8].replace(' ','') not in list_chongfu_3_1:
                list_chongfu_3_2.append(ii)
        count_cf = 0
        list_cf_1=list_chongfu_3_2
        list_cf_2=[]
        list_cf_2_1=[]
        list_cf_3=[]
        list_cf_1=set(list_cf_1)#粗略去重
        for i in list_cf_1:
            list_cf_2.append(i[:8])
        list_cf_2=set(list_cf_2)
        for x in list_cf_2:
            list_cf_2_1.append(x)
        for ii in list_cf_1:
            count_cf=0
            for iii in list_cf_2_1:
                if ii[:8]==iii:
                    list_cf_3.append(ii)
                    list_cf_2_1.pop(count_cf)
                count_cf=count_cf+1                                   
        for y in list_cf_3:
                if y[0]=='n' and y[1]!='n' and y[2]!='n':
                            new_ti_init=y.replace(y[0],str(count_chongfu))
                if y[0]=='n' and y[1]=='n' and y[2]!='n':
                            new_ti_init=y.replace(x[0]+y[1],str(count_chongfu))
                if y[0]=='n' and y[1]=='n' and y[2]=='n':
                            new_ti_init=y.replace(y[0]+y[1]+y[2],str(count_chongfu))
                list_chongfu_4.append(new_ti_init)
                count_chongfu=count_chongfu+1
        count_chongfu=0
        chongfu_jianda=Document()
        for chongfu_jd in list_chongfu_4:
            new=chongfu_jianda.add_paragraph(chongfu_jd)
        chongfu_jianda.save(str(os.path.dirname(os.path.realpath(sys.argv[0]))) + '\\简答题题库.docx')   
        print('【简答题】题库重复题已经概略清除，如果要严格清除重复题，请手动打开题库文件并清除')
    
    return


#############主程序################################################      
def homepage():   
    print('{:*^50}'.format('***'))#居中显示
    print('{:*^43}'.format('自动化试卷生成v2.9'))#居中显示
    print('{:*^50}'.format('Rainbow6'))
    print('{:*^50}'.format('***'))#居中显示
    print('{:*^5}'.format('！！！注意：对于选择题中选项为ABCD的选项，同时内容中也有ABCD时会产生意外的错误！！！'))#居中显示    
    print('{:*^5}'.format('！！！注意：因此，生成试卷后请检查试卷格式，并将错误格式的题目手动清除替换！！！'))#居中显示  
    print('{:*^5}'.format('！！！注意：同时在选择题题库中尽量不要出现此类题目,或在选项中使用小写字母！！！'))#居中显示  
    print('\n请检查题库是否存放在以下地址： '+os.path.dirname(os.path.realpath(sys.argv[0])))
while True:
    homepage_flag=homepage_flag+1
    if homepage_flag==1:
        init_tiku()
        remove_chongfu()
        
        if(sys.platform=='win32'):#判断使用者的系统是windows 还是 Linux
               os.system("cls")
        elif(sys.platform=='linux'):
               os.system('clear')
        
        homepage()
    gongneng_flag=(input('\n请输入对应数字选择需要的功能：\n\n1、创建新的试卷     2、题库管理与创建     3、退出     4、清屏     1+4、反映Bug\n'))
    if(gongneng_flag.isdigit()==False):
        print('输入错误，请重新输入，注意输入数字！')
    else:
        if(int(gongneng_flag)==1):
            creat_newpaper()
        elif(int(gongneng_flag)==2):
            add_q_bank()
        elif(int(gongneng_flag)==3):
            delay_i=3
            for x in range(3):
                print('\r谢谢使用，'+str(delay_i)+'秒钟后将自动退出程序...',end='')#不换行输入显示倒计时，\r表示回车，即每次输入换行，但是末尾end=''将换行取消
                delay_i=delay_i-1                                                       #所以两个组合表示将输入光标移动到行首，并且不换行
                time.sleep(1)
            sys.exit(0)
        elif(int(gongneng_flag)==4):
            #print(sys.platform)
            if(sys.platform=='win32'):#判断使用者的系统是windows 还是 Linux
                os.system("cls")
                homepage()
            elif(sys.platform=='linux'):
                os.system('clear')
                homepage()
        elif (int(gongneng_flag)==5):
            print('{:-^50}'.format("感谢您提供宝贵意见"))
            print('{:-^50}'.format("详情请发送邮件到rainbowno6@163.com联系软件作者"))
            delay_i=3
            for x in range(3):
                print('\r再次感谢您，'+str(delay_i)+'秒钟后返回主菜单...',end='')#不换行输入显示倒计时，\r表示回车，即每次输入换行，但是末尾end=''将换行取消
                delay_i=delay_i-1                                                       #所以两个组合表示将输入光标移动到行首，并且不换行
                time.sleep(1)
        else:
            print('功能选择错误，请重新输入\n')

    
